如果我有一个用于检索记录的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次计数。
答案 0 :(得分:4)
$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>';
}
答案 1 :(得分:1)
<?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)
?>