我想将 php mysql 中的数据拆分为每列 20 行。下面是我的代码:
while($row = mysql_fetch_assoc($result)) {
$id[] = $row['id'];
}
for($i = 0; $i < 20; $i++) {
echo '<table><tr>';
for ($j = 0; $j < (count($id)-1)/20; $j++) {
echo '<td>'.$id[$i + $j * 20].'</td>';
}
echo '</tr><tr>';
}
echo '</tr></table>';
假设我有 21 行,代码只输出 20 行,并且缺少第 21 行。
而如果有22行(只要除以20后的余数大于或小于1),就有 undefined offset 22、 undefined offset 23......
错误。
内部 for
循环有问题,但我无法修复它。我该如何修复这个错误?
最佳答案
我无法发表评论(+50 代表)并在此处添加一些评论
while($row = mysql_fetch_assoc($result)) {
$id[] = $row['id'];
}
for($i = 0; $i < 20; $i++) {
echo '<table><tr>';
for ($j = 0; $j < (count($id)-1)/20; $j++) {
echo '<td>'.$id[$i + $j * 20].'</td>';
}
echo '</tr><tr>';
}
echo '</tr></table>';
我不知道为什么使用第二个 for
但在 for 条件下,您为 for
定义了 20 次运行
这段代码也许对你有帮助
echo '<table>'
while($row = mysql_fetch_assoc($result)) {
$id[] = $row['id'];
}
for($i = 0; $i <= 20; $i++) {
echo '</tr><td>'.$id[$i].'</td><tr>';
}
echo '</table>';
这是第 21 次运行 for
与 <=
关于php - 将php mysql数据拆分为每列20行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546695/