避免使用php显示具有空值的行
我不需要显示具有null值的行,例如,如果$Banner没有值,我的意思是null,那么我将跳过显示filmBanner并回显下一行Distributor 这是我的密码: 所需输出为: 如果banner=null,则跳过banner并显示下一列。所以这里我想在表本身而不是sql查询中编写循环 这是我以前使用的代码,运行得很好,但显示的是空值 现在,我将整个替换为以下内容:避免使用php显示具有空值的行,php,mysql,Php,Mysql,我不需要显示具有null值的行,例如,如果$Banner没有值,我的意思是null,那么我将跳过显示filmBanner并回显下一行Distributor 这是我的密码: 所需输出为: 如果banner=null,则跳过banner并显示下一列。所以这里我想在表本身而不是sql查询中编写循环 这是我以前使用的代码,运行得很好,但显示的是空值 现在,我将整个替换为以下内容: <?php $sql = "SELECT * FROM films"; $result = mysql_query($
<?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();
?>
我会用这样的方式:
<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>
您将在代码中找到此$$column[0],这意味着代码将添加一个包含$column[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 ?>
其中column=NULL?或者循环,如果$row['column']==NULL{…}类型的东西。请。它们不再得到维护,而是在使用。看到了吗?而是学习,并使用或。将帮助您决定哪一个。@JayBlanchard无需再次执行所有操作,只需输入几下键盘,然后执行一个-我将在大约一分钟内删除此内容lol@JayBlanchard后来我就想到了,;无聊的。。。让他们像无头鸡一样到处乱跑。OAN-你是如何得到评论的URL的?等等,没关系。嗯,我没有运行实际的代码,显然没有数据库。最明显的是使用了echo的缩写形式user3980820,我可以将abobe3列组合在一起吗?比如:if$Banner=零,$Distributor=零美元剧本=空,然后一起显示数据?因为我有大约20多列,这样我就必须分别编写每一列。user3980820,我得到了内部服务器错误,所以我会错过这样一个变量:$Banner=$column['Banner']$Distributor=$column['Distributor']$剧本=$column[“剧本”];用新代码替换while部分中的所有代码。我认为有问题,目前我将使用您的第一个代码,这也符合我的目的。唯一的问题是我必须为每个列分别编写。请检查上面的代码。我已替换了我的工作代码,该代码甚至会抛出空值,如果有错,请纠正我
<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>
<?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
}
}
}
?>
<?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 ?>