避免使用php显示具有Null值的行

时间:2014-09-26 15:59:07

标签: php mysql

我不想显示具有空值的行,例如,如果“$ Banner”没有值(我的意思是Null),那么我将跳过显示filmBanner和echo下一行称为“Distributor”

这是我的代码:

$sql = "SELECT * FROM films ASC LIMIT 0 , 30";
$result = mysql_query($sql);
?>
while($row = mysql_fetch_array($result))
{
   $Banner=$row['Banner'];
    $Distributor=$row['Distributor'];
    $Screenplay=$row['Screenplay'];    
<table>
      <tr>
            <td><b>Banner / Studio:</b></td>
            <td><?php echo"$Banner";?></td>
        </tr>
        <tr>
            <td><b>Distributed by:</b></td>
            <td><?php echo"$Distributor";?></td>
        </tr>
        <tr>
            <td><b>Screenplay</td>
            <td><?php echo"$Screenplay";?></td>
        </tr>

    </table>

输出需要: 如果banner = null则跳过横幅并显示下一列。所以在这里我想在“表”中编写循环而不是在sql查询中。

这是我之前使用的代码,运行完美,但显示的是空值

<?php
    $sql = "SELECT * FROM films";
    $result = mysql_query($sql);    
    while($row = mysql_fetch_array($result))
    {
       $filmBanner=$row['filmBanner'];
       $filmDistributor=$row['filmDistributor'];
       $filmScreenplay=$row['filmScreenplay'];
    ?>  
    <table>
          <tr>
                <td><b>Banner / Studio:</b></td>
                <td><?php echo"$filmBanner";?></td>
            </tr>
            <tr>
                <td><b>Distributed by:</b></td>
                <td><?php echo"$filmDistributor";?></td>
            </tr>
            <tr>
                <td><b>Screenplay</td>
                <td><?php echo"$filmScreenplay";?></td>
            </tr>

        </table>
    <?php
    }
    mysql_free_result($result);
    mysql_close();
    ?>

现在我将整个替换为以下内容:

<?php
$sql = "SELECT * FROM films";
$result = mysql_query($sql);
?>
<?php 
    $columns = [["filmBanner","Banner / Studio:"],["filmDistributor","Distributed by:"],["filmScreenplay","Screenplay"]];
    foreach($columns as $column){
        $$column[0] = $row[$column[0]];
        If($$column[0]!=null){
        ?>
        <tr>
        <td><b><?php echo $column[1]; ?></b></td>
        <td><?php echo $$column[0];?></td>
        </tr>
        <?php
        }

    }
mysql_free_result($result);
mysql_close();
?>

2 个答案:

答案 0 :(得分:0)

我会用这样的东西:

<table>
    <?php
       if($Banner!=null){
            echo "<tr>";
            echo "<td><b>Banner / Studio:</b></td>";
            echo "<td>$Banner</td>"
            echo "</tr>";

        }
    ?>
    <tr>
        <td><b>Distributed by:</b></td>
        <td><?php echo"$Distributor";?></td>
    </tr>
    <tr>
        <td><b>Screenplay</td>
        <td><?php echo"$Screenplay";?></td>
    </tr>

</table>

对于您拥有的每个列,您需要在此数组$columns上创建一个值,它应该如下[Nameoftherowvariable,Valuetoshowbeforethevariable]所以这是一个示例:

<?php 
while($row = mysql_fetch_array($result))
{
    $columns = [["Banner","Banner / Studio:"],["Distributor","Distributed by:"],["Screenplay","Screenplay"]];
    foreach($columns as $column){
        $$column[0] = $row[$column[0]];
        If($$column[0]!=null){
        ?>
        <tr>
        <td><b><?php echo $column[1]; ?></b></td>
        <td><?php echo $$column[0];?></td>
        </tr>
        <?php
        }

    }
}
?>

您会在代码中找到此$$column[0],这意味着代码将添加一个包含在$column[0]中的文本的变量。我希望这会对你有所帮助 您可以在此link找到以下代码和使用的代码的结果。

答案 1 :(得分:0)

您需要在输入“tr”元素之前测试变量的值,如果它们为null则不打印。我还建议不要为你想要的每一列写出行。下面是创建一个包含所需列及其标签的数组的代码;然后根据列的值是否为空打印它们。这应该涵盖NULL,0

<?php
$sql = "SELECT * FROM films ASC LIMIT 0 , 30";
$result = mysql_query($sql);

// Fill with the desired column names, and their table labels
$columns = array(
    array('col'=>'Banner','label'=>'Banner / Studio'),
    array('col'=>'Distributor','label'=>'Distributed by:'),
    array('col'=>'Screenplay','label'=>'Screenplay')
);
?>

<?php while ($row = mysql_fetch_array($result) ) : ?>
    <table>
        <!-- For every column, test the value. If not empty / null, print a table row -->
        <?php foreach ( $col in $columns ) : ?>
        <?php if ( !empty($row[ $col->col ]) ) : ?>
            <tr>
                <td><b><?= $col->label ?></b></td>
                <td><?= $row[ $col->col ] ?></td>
            </tr>
        <?php endif ?>
        <?php endforeach ?>
    </table>
<?php endwhile ?>