mysql查询结果分组

发布于 2024-11-09 10:13:42 字数 1149 浏览 0 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清欢 2024-11-16 10:13:42

您需要让 mod % 关闭 li 或打开 li,而不是处理 div

所以像这样:

$counter = 1;
while($row = mysql_fetch_array($result)) {
    if ( $counter % 5 == 1 ) {           
        echo '<li>';
    }
    echo '<div class="title">'.$row['title'].'</div>';
    if ( $counter % 5 == 0 ) {           
        echo '</li>';
    }
    $counter ++;
}
if ( $counter % 5 != 1 ) {           
    echo '</li>';
}

还没有测试过它,但它应该可以帮助您走上正确的道路。

You need to have your mod % either close the li or open the li, not deal with the div.

So something like this:

$counter = 1;
while($row = mysql_fetch_array($result)) {
    if ( $counter % 5 == 1 ) {           
        echo '<li>';
    }
    echo '<div class="title">'.$row['title'].'</div>';
    if ( $counter % 5 == 0 ) {           
        echo '</li>';
    }
    $counter ++;
}
if ( $counter % 5 != 1 ) {           
    echo '</li>';
}

Haven't tested it, but it should help set you on the right path.

鯉魚旗 2024-11-16 10:13:42

你们看起来很亲密,怎么样:

$counter = 0;
while($row = mysql_fetch_array($result)){
        if (($counter % 5) == 0) {
            echo '<li>';
        }
        echo '<div class="title">'.$row['title'].'</div>';
        if (($counter % 5) == 4) {
            echo '</li>';
        }
        $counter ++;
    }
}
if ($counter % 5 != 4) {           
    echo '</li>';
}

You seem very close, how about:

$counter = 0;
while($row = mysql_fetch_array($result)){
        if (($counter % 5) == 0) {
            echo '<li>';
        }
        echo '<div class="title">'.$row['title'].'</div>';
        if (($counter % 5) == 4) {
            echo '</li>';
        }
        $counter ++;
    }
}
if ($counter % 5 != 4) {           
    echo '</li>';
}
如梦亦如幻 2024-11-16 10:13:42
$x = mysqli_num_rows($result);   
$counter = 0;

while($row = mysql_fetch_array($result)){
if ( ($counter % 5) == 0 ){
echo '<li>'
}           
echo '<div class="title">'.$row['title'].'</div>';
if (((($counter +1) % 5) == 0) || (($counter+1)==$x)){
echo '</li>'
} 
$counter ++;
}

我没有检查它,但 mysqli_num_rows 变量可用于关闭最后一个。

$x = mysqli_num_rows($result);   
$counter = 0;

while($row = mysql_fetch_array($result)){
if ( ($counter % 5) == 0 ){
echo '<li>'
}           
echo '<div class="title">'.$row['title'].'</div>';
if (((($counter +1) % 5) == 0) || (($counter+1)==$x)){
echo '</li>'
} 
$counter ++;
}

I did not check it but the mysqli_num_rows variable can be used to close last

.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文