1

我正在从数据库中打印出一张包含 27 列的表格,因此很明显,如果在我的屏幕上可以看到 27 列,这将在美学上令人不快。所以这是我一直用来查看特定 col 是否为空的条件之一,如果它为空,则不会打印表头,如果没有打印另一个 if isset 条件将不会打印表数据。但它并没有按计划进行。这些是我尝试过的变体,但它们都不起作用 PS $result = 查询返回的行数。

$i = 1;
while ($i <= $result)

    {
        if (!empty($array['Others'][$i]))
            {

                $others = print "<th>Others</th>";
                break;
            }
        $i++;
    }


$i = 0;
    while ($i <= $result)
    {
        $emptyothers = !empty($array['Others'][$i]);

        if ($emptyothers == '1')
            {

                $others= print "<th>Others</th>";
                break;
            }
        $i++;
    }   
4

2 回答 2

5

Your code should be like this:

$sql = mysql_query("SELECT * FROM table");
if (mysql_num_rows($sql) > 0) {
    //your code...
} else {
    print 'is empty';
}
于 2012-05-18T19:01:15.307 回答
1

你能用array_key_exists()吗?

foreach($row in $result) {
    if(array_key_exists('Others', $row)) {
        if(!empty($row['Others']) {
            print "<th>Others</th>";
            break;
        }
    }
}
于 2012-05-18T19:03:33.997 回答