每行仅显示 3 个 foreach 结果
我有如下脚本
$output="<table class='products'><tr>";
while($info = mysql_fetch_array( $data )) {
//Outputs the image and other data
$output.= "<td>
<img src=http://localhost/zack/sqlphotostore/images/" .$info['photo'] ." width=323px ></img>
<b>Name:</b> ".$info['name'] . "
<b>Email:</b> ".$info['email'] . "
<b>Phone:</b> ".$info['phone'] . "</td> ";
}
$output.="<tr></table>";
print $output;
?>
它以长水平线显示所有结果我如何打破结果以便它们显示3 次后在新行中.
it shows all results in long horizontal line how do i break the results so that they show in new row after 3 count.
推荐答案
添加一个计数器并在每次达到 3 的倍数时开始新行.
Add a counter and start a new row every time it reaches a multiple of 3.
$counter = 0;
while($info = mysql_fetch_array($data)) {
if ($counter++ % 3 == 0) {
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "<tr>";
}
// stuff
}
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "</table>";
请注意:它可能无法回答您的问题,但您应该停止使用 mysql_*
函数.它们正在被弃用.而是使用 PDO(自 PHP 5.1 起支持)或 mysqli(自 PHP 4.1 起支持).如果您不确定使用哪个,阅读这篇文章.
Please note: It may not help answer your question, but you should stop using mysql_*
functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
相关文章