【发布时间】:2010-02-02 12:09:45
【问题描述】:
我在 PHP 中有这个循环,它会回显一个结果表,
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<!--<td><b>Uploaded By</b></td>-->
</tr>
<?php $colours = array("#f9f9f9", "#f3f3f3"); $count = 0;?>
<?php foreach ($allContentImages as $contentImages) : ?>
<tr bgcolor="<?php echo $colours[$count++ % count($colours)];?>">
<td><?php echo "<a href='#' class='screenshot' rel='/media/uploads/$contentImages[categoryId]/$contentImages[contentImageName]'>".$contentImages['contentImageName']; ?></td>
<td><?php echo $contentImages['contentImageType']; ?></td>
<td><?php if($contentImages['isHeadlineImage'] == 1){ echo "Y";}else{echo "N";} ?></td>
<td><?php echo $contentImages['contentTitle'] ?></td>
<td><?php echo date("d-m-Y", $contentImages['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete" href="<?php echo base_url();?>dashboard/deleteContentImage/<?php echo $contentImages['contentImageId'];?>"><img src="/media/images/icons/cancel.png" alt="Delete A Category"/></a></td>
</tr>
<?php
if($contentImages['isHeadlineImage'] == '0') {
echo "<tr bgcolor='red'>";
echo "<td><p>You need to assign a headline image</p></td>";
echo "</tr>";
?>
<?php endforeach; ?>
</table>
我需要检查每个具有相同标题的内容片段是否有标题图片,如果没有,则回显一个红色的新行...但我得到的只是每次有图片时都会有一个新行不是标题图片。谁能帮我?如果这有助于匹配 td 的值,我不介意使用 javascript?但显然我的尝试是不正确的。
【问题讨论】:
-
你为什么要跳进跳出这么多 PHP 代码?如果您只是将一小部分 HTML 包含到您的 PHP 字符串中,或者预先计算值而不是内联进行计算,那么它的可读性会大大提高。
-
或者,我的偏好是反过来:将
<a href='#'...和<tr bgcolor字符串踢到主要内容中。无论哪种方式,您都需要在输出到 HTML 的任何文本内容上调用htmlspecialchars,否则您会遇到 XSS 漏洞。