mysql查询结果分组
从 mysql 查询中,我希望将 5 个结果作为一组包装到 li
中。怎样?谢谢,
$counter = 1;
while($row = mysql_fetch_array($result)){
echo '<li>';
if ( ($counter % 5) == 0 ){
echo '<div class="title">'.$row['title'].'</div>';
}
$counter ++;
echo '</li>';
}
}
我想要一些 html 输出,例如
<li><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div></li>
<li><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div></li>
<li><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div></li>
... <!-- 5 results in one li -->
From mysql query, I want make 5 results as a group wrap into a li
. how to? thanks
$counter = 1;
while($row = mysql_fetch_array($result)){
echo '<li>';
if ( ($counter % 5) == 0 ){
echo '<div class="title">'.$row['title'].'</div>';
}
$counter ++;
echo '</li>';
}
}
I want some html output like
<li><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div></li>
<li><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div></li>
<li><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div><div class="title">something</div></li>
... <!-- 5 results in one li -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要让 mod % 关闭
li
或打开li
,而不是处理div
。所以像这样:
还没有测试过它,但它应该可以帮助您走上正确的道路。
You need to have your mod % either close the
li
or open theli
, not deal with thediv
.So something like this:
Haven't tested it, but it should help set you on the right path.
你们看起来很亲密,怎么样:
You seem very close, how about:
我没有检查它,但 mysqli_num_rows 变量可用于关闭最后一个。
I did not check it but the mysqli_num_rows variable can be used to close last
.