【发布时间】:2010-12-29 10:34:15
【问题描述】:
我需要从随机数 $totalItems 生成随机数的 div,每个 div 有五个项目(以及最后一个 div 中的剩余项目),而且并非所有项目都满足 $OKItems... 希望代码比我解释得更好.
我的问题是这个脚本生成了没有内容的空 div。
<?php
$OKItems = 0;
$totalItems = rand(2,30);
for ($i = 0; $i < $totalItems; $i++) {
echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br />' : '';
$testValue = rand(0, 1);
if ($testValue != 0) {
echo '1';
$OKItems++;
}
echo ($OKItems % 5 == 0 || $i+1 == $totalItems) ? '<br />div close<br />' : '';
}
?>
这是我可能得到的:
div open
div close
div open
11111
div close
div open
div close
div open
div close
div open
11
div close
这就是我在这种情况下想要的:
div open
11111
div close
div open
11
div close
【问题讨论】:
-
请选择一个给定的答案并接受它