display only 3 foreach result per row(每行仅显示 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.
这篇关于每行仅显示 3 个 foreach 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:每行仅显示 3 个 foreach 结果


- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01