-1

当回显 fetch_assoc 的结果时,我想跳过空(NULL)结果。我现在的代码是:

while ($rowsoft = $resultsoft->fetch_assoc()) {
echo "<table class='swtable' cellspacing='0' summary='Software table'>
<tbody>
<tr><td class='software_up'><a class='sw' href='software.php?id=" . $rowsoft['id'] . "'>" . $rowsoft['title'] . "</a></td></tr>
<tr><td class='software'><div class='tags'>Tags: </div><a class='tags' href='#'>" . $rowsoft['tag_1'] . "</a>
<a class='tags' href='#'>" . $rowsoft['tag_2'] . "</a><a class='tags' href='#'>" . $rowsoft['tag_3'] . "</a></td></tr>
}

但并非总是有 $rowsoft['tag_1'] 或 $rowsoft['tag_2'] 或 $rowsoft['tag_3'] 的结果。如何更改 fetch_assoc() 内部的回显,使其仅在有结果时才起作用?或者我应该使用 foreach 还是其他东西?

谢谢

4

2 回答 2

0

正如@Musa 所说,您可以将它们从查询中排除。这可能是数据库在做这项工作的最佳方式。您还可以使用一堆 if 语句测试 $rowsoft['tag_1'] 是否为空,然后不执行回显。

于 2013-08-18T21:09:37.243 回答
0

我通过更改 sql 得到它:

select
...    
    (select concat('<a href=\'#\' class=\'tags\'>',t.tag_bg,'</a>') from tags t where t.id=s.tag1) tag_1,
    (select concat('<a href=\'#\' class=\'tags\'>',t.tag_bg,'</a>') from tags t where t.id=s.tag2) tag_2,
    (select concat('<a href=\'#\' class=\'tags\'>',t.tag_bg,'</a>') from tags t where t.id=s.tag3) tag_3
from software where...

然后在 fetch_assoc() 中:

<tr><td class='software'><div class='tags'>Tags:</div>" . $rowsoft['tag_1'] . "" . $rowsoft['tag_2'] . "" . $rowsoft['tag_3'] . "</td></tr>

效果很好,谢谢建议!

于 2013-08-19T11:40:12.783 回答