Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中表内的if函数_Php - Fatal编程技术网

php中表内的if函数

php中表内的if函数,php,Php,我是php新手,请帮我解决我的问题 我有一个使用php生成的表。在我的表格的Download列中,我有一个条件来检查某个文件是否存在。如果是,则生成其超链接以供下载 请检查我的密码 if (isset($_POST['action']) && $_POST['action'] == "view") { $output = ''; $data = $admin->read(); if ($admin->totalRowCoun

我是php新手,请帮我解决我的问题

我有一个使用php生成的表。在我的表格的
Download
列中,我有一个条件来检查某个文件是否存在。如果是,则生成其超链接以供下载

请检查我的密码

if (isset($_POST['action']) && $_POST['action'] == "view") {
    $output = '';
    $data = $admin->read();
    if ($admin->totalRowCount() > 0) {
        $output .= '<table class="table table-striped table-sm table-bordered">
            <thead>
              <tr class="text-center">
                <th>ID</th>
                <th>Nama</th>
                <th>download</th>
              </tr>
            </thead>
            <tbody>';
        foreach ($data as $row) {
            $output .= '<tr class="text-center text-secondary">
            <td>' . $row['id'] . '</td>
            <td>' . $row['nama'] . '</td>
            <td>

               //this is my if file exists function, how to make it work??
               if (file_exists( __DIR__ . "upload/' . $row['nama'] . '.xlsx")) {
                      echo "<a href="upload/' . $row['nama'] . '.xlsx" download>tes</a>";
                    }else {
                      echo "not exists";
                    }

            </td></tr>';
        }
        $output .= '</tbody></table>';
        echo $output;
    } else {
        echo '<h3 class="text-center text-secondary mt-5">:( Empty!</h3>';
    }
}
if(设置($\u POST['action'])和&$\u POST['action']==“查看”){
$output='';
$data=$admin->read();
如果($admin->totalRowCount()>0){
$output.='
身份证件
纳米
下载
';
foreach($行数据){
$output.='
“.$row['id']”
“.$row['nama']”
//这是我的如果文件存在的功能,如何使它工作??
如果(文件存在(uuu DIR_uuuu.“upload/”.$row['nama']..xlsx”)){
回声“;
}否则{
回声“不存在”;
}
';
}
$output.='';
echo$输出;
}否则{
回声':(空的!';
}
}

您需要先结束
$output.=…
行,然后继续
$output.='';
if
之后的行中。另外,将
if
中的
回显更改为
$output.=…

$output .= '<tr class="text-center text-secondary">
    <td>'.$row['id'].'</td>
    <td>'.$row['nama'].'</td>
    <td>'; // end $output here

if (file_exists( __DIR__ . "upload/" . $row['nama'] . ".xlsx")) {
    $output .= '<a href="upload/' . $row['nama'] . '.xlsx" download>tes</a>';
} else {
    $output .= "not exists";
}

// resume $output from here
$output .= '</td></tr>';
$output.='
“.$row['id']”
“.$row['nama']”
“;//在此处结束$output
如果(文件存在(uuuu DIR_uuuuu..upload/“$row['nama']..xlsx”)){
$output.='';
}否则{
$output.=“不存在”;
}
//从这里恢复$output
$output.='';

编辑:还修复了
中的引号,如果

您的代码只需要正确的代码结构和引号安排。另外,
$output
中的字符串连接不正确/格式不正确,
kerbh0lz
在他的回答中已经提到了这一点

然而,这里是您的完整代码

if (isset($_POST['action']) && $_POST['action'] == "view") {
    $output = '';
    $data = $admin->read();
    if ($admin->totalRowCount() > 0) {
        $output .= '<table class="table table-striped table-sm table-bordered">
            <thead>
              <tr class="text-center">
                <th>ID</th>
                <th>Nama</th>
                <th>download</th>
              </tr>
            </thead>
            <tbody>';
        foreach ($data as $row) {
            $output .= '<tr class="text-center text-secondary">
            <td>' . $row['id'] . '</td>
            <td>' . $row['nama'] . '</td>
            <td>';
            //this is my if file exists function, how to make it work??
            if (file_exists(__DIR__ . 'upload/' . $row['nama'] . '.xlsx')) {
                $output .= '<a href="upload/' . $row['nama'] . '.xlsx" download>test</a>';
            } else {
                $output .= "not exists";
            }
            $output .= '</td></tr>';
        }
        $output .= '</tbody></table>';
        echo $output;
    } else {
        echo '<h3 class="text-center text-secondary mt-5">:( Empty!</h3>';
    }
}
if(设置($\u POST['action'])和&$\u POST['action']==“查看”){
$output='';
$data=$admin->read();
如果($admin->totalRowCount()>0){
$output.='
身份证件
纳米
下载
';
foreach($行数据){
$output.='
“.$row['id']”
“.$row['nama']”
';
//这是我的如果文件存在的功能,如何使它工作??
如果(文件存在(uuu DIR_uuu.'upload/'.$row['nama']..xlsx')){
$output.='';
}否则{
$output.=“不存在”;
}
$output.='';
}
$output.='';
echo$输出;
}否则{
回声':(空的!';
}
}

您得到的错误是什么?我的猜测是路径中缺少
/
。要验证这一点,请执行两件事。将
echo更改为“不存在”;
更改为
echo“不存在”;\uu DIR\uuu。“upload/”。$row['nama']..xlsx;
查看您尝试调用的路径。然后更改为
/upload
(在上传之前添加斜杠)你对这个代码有什么问题?有什么问题吗?