我正在编写一个小购物车,目前正面临如何将MySQL表结果显示到表格显示中的问题。我有一个代码可以执行整个数据库搜索并返回结果,我的问题是像大多数购物车一样显示结果。
以下是我想要实现的目标的一个例子。
<table>
<tr>
<td>First mysql result row comes here</td>
<td>Second Mysql result row comes here</td>
</tr>
<tr>
<td>third mysql result row comes here</td>
<td>fourth Mysql result row comes here</td>
</tr>
</table>
请注意,它每行显示两个<td>
。
我很难找到答案。可以使用 css 完成吗?
<table>
<?php
$tr = false;
foreach( $results_from_database as $result ) {
$tr = !$tr;
if( $tr ) {
echo '<tr>';
}
echo "<td>$result</td>";
if( !$tr ) {
echo '</tr>';
}
}
// if there's odd amount of results, add an empty cell and close the tr tag
if( $tr ) {
echo '<td></td></tr>';
}
?>
</table>