分隔记录 PHP

发布于 2024-10-03 01:42:42 字数 407 浏览 0 评论 0原文

如果我有一个检索记录的 while 循环,我希望能够通过在循环进行时将记录包装在一定数量的记录之后来分隔记录,例如

(使用 while 循环):

Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record 7

但我需要像这样对记录进行分组:

<div class="wrap">
Record 1
Record 2
Record 3
</div>
<div class="wrap">
Record 4
Record 5
Record 6
</div>
Record 7

所以当它超过 3 时,它应该每 3 个计数换行。

If I have a while loop that retrieve records, I want to be able to delimit records by wrapping them after an amount of records while the loop is going, e.g.

(using a while loop):

Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record 7

But I need to group records like this:

<div class="wrap">
Record 1
Record 2
Record 3
</div>
<div class="wrap">
Record 4
Record 5
Record 6
</div>
Record 7

So when it exceeds more than 3 it should wrap every 3 count.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青柠芒果 2024-10-10 01:42:42
$index = 0;

while (...) {
    if ($index == 0) {
        echo '<div class="wrap">';
    } elseif (($index % 3) == 0) {
        echo '</div><div class="wrap">';
    }

    // Output your stuff

    $index++;
}

if ($index != 0) {
    echo '</div>';
}
$index = 0;

while (...) {
    if ($index == 0) {
        echo '<div class="wrap">';
    } elseif (($index % 3) == 0) {
        echo '</div><div class="wrap">';
    }

    // Output your stuff

    $index++;
}

if ($index != 0) {
    echo '</div>';
}
土豪我们做朋友吧 2024-10-10 01:42:42
<?php

// Dummy data
$records = array('1','2','3','4','5','6','7');

// While we have at least 3 records, group them
while (count($records) > 3) {
     $subs = array_splice($records,0,3);
     print '<div class="wrap">'.implode(PHP_EOL, $subs).'</div>';
}

// Dump the rest
print implode(PHP_EOL, $records)

?>
<?php

// Dummy data
$records = array('1','2','3','4','5','6','7');

// While we have at least 3 records, group them
while (count($records) > 3) {
     $subs = array_splice($records,0,3);
     print '<div class="wrap">'.implode(PHP_EOL, $subs).'</div>';
}

// Dump the rest
print implode(PHP_EOL, $records)

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