Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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中有多个单元格为空,则跳过行_Php - Fatal编程技术网

如果PHP中有多个单元格为空,则跳过行

如果PHP中有多个单元格为空,则跳过行,php,Php,我有一个从CSV文件生成HTML表的PHP脚本 现在,如果任何单元格缺少信息,它将跳过该行。但是,如果缺少多个单元格,我更希望它跳过一行因此,如果一行有2个空单元格,它应该跳过。 我已经在下面标记了//在这里编辑,但不确定如何实现 <?php $idsColumnsWanted = array(0,1,8,19); echo "<table class='table table-bordered' id='example'>\n\n"; $f = fopen("users.

我有一个从CSV文件生成HTML表的PHP脚本

现在,如果任何单元格缺少信息,它将跳过该行。但是,如果缺少多个单元格,我更希望它跳过一行因此,如果一行有2个空单元格,它应该跳过。

我已经在下面标记了
//在这里编辑
,但不确定如何实现

<?php
$idsColumnsWanted = array(0,1,8,19);

echo "<table class='table table-bordered' id='example'>\n\n";

$f = fopen("users.csv", "r");
$first_line = false;

while (($line = fgetcsv($f)) !== false) {
    // Restart column index
    $i = 0;
    $row ="";

    if($first_line == false) {
        $row = "<thead><tr>";
        $col = "th";
    } else {
        $row = "<tr>";
        $col= "td";
    }

    $is_empty = false;
    foreach ($line as $i => $cell) {
        // Skips all columns not in your list
        if (! in_array($i, $idsColumnsWanted)) continue;

        // edit here
        if ($cell !== '') {
            $row .= "<".$col.">" . htmlspecialchars($cell) . "   </".$col.">";
        } else {
            $is_empty = true;
        }
        // Increase index
        $i++;
    }

    if($first_line == false)
        $row .= "</tr></thead>";
    else 
        $row .= "</tr>";

    $first_line = true;

    if ($is_empty) {
        continue;
    } else {
        echo $row;
    }
}
fclose($f);
echo "\n</table>";
?>
请尝试此代码

$is_empty = false;
$count = 0;
$countlimit = 2; // define empty cell limit here
foreach ($line as $i => $cell) {
    // Skips all columns not in your list
    if (!in_array($i, $idsColumnsWanted))
        continue;

    // edit here
    if($cell==""){
        $count++;
    }

    if ($count <= $countlimit) {
        $row .= "<" . $col . ">" . htmlspecialchars($cell) . "   </" . $col . ">";
    } else {
        $is_empty = true;
        break;
    }

}
$is_empty=false;
$count=0;
$countlimit=2;//在此定义空单元格限制
foreach($i=>$cell的行){
//跳过列表中不包含的所有列
if(!in_数组($i,$ids))
继续;
//编辑这里
如果($cell==“”){
$count++;
}

如果($count您必须计算一行中有多少个单元格是空的。为此,请初始化每行的计数器

$empty_cells = 0; //Init with 0 for each line
foreach ($line as $i => $cell) {
    // Skips all columns not in your list
    if (! in_array($i, $idsColumnsWanted)) continue;

    // edit here
    if ($cell !== '') {
        $row .= "<".$col.">" . htmlspecialchars($cell) . "   </".$col.">";
    } else {
        $empty_cells++; //count how many empty cells you have.
    }
    // Increase index
    $i++;
}

使用计数器而不是布尔变量,当单元格为空时增加计数器,并检查循环后计数器是否大于1。现在开始:。如果有多个值(单元格)在“行为空”中,将$is_empty设置为true。@PatrickAleman不要在任何地方都使用向下投票,而是显示一些代码来演示您的想法相关说明:您不需要在
foreach
循环的末尾执行
$i++;
位-循环构造在每次迭代开始时为您设置
$i
的值on.@PatrickAleman有一个很好的观点。在处理每一行之前使用
数组\u计数\u值
,不仅可以得到更简洁的代码,而且还可以避免构建HTML字符串(
$row
变量)后来才放弃。虽然如果Patrick发布一个答案来证明这一点会更好,而不仅仅是一条评论……我不得不更改
$countlimit=1;
,因为它从
0开始,所以它变成了
0,1
,因此2。谢谢,@Rakesh:-DNo我认为它是正确的,因为它先递增,然后是ch选中条件$count设置
$is_empty=true;
之后,您可能想打破
foreach
的限制;
此外,您实际上不需要
$cell!==''|
位,只要
$count我希望您不介意,但我也编辑了它:
$I++
行完全没有必要(根据我在问题下的评论)。此外,您确实需要有
$is\u empty=true;
以及
中断
,因为
$is\u empty
是在
foreach
循环之后测试的变量,以决定是否
回显$row
@daiscog:是的。谢谢您的更正。在
$empty\u cells++
之后,如果需要,请添加
($empty_cells==2)break;
这样脚本就不必在不使用的行中循环。
if ($empty_cells >= 2) {
    continue;
} else {
    echo $row;
}