对于PHP中的循环表 [英] For Loop Table in PHP

查看:57
本文介绍了对于PHP中的循环表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php for loop生成一个表,该表列出了数字.像这样:

I am trying to generate a table with php for loop, that lists numbers. Something like this:

1 | 2 | 3 | 4 | 5
2 | 3 | 4 | 5 | 1
3 | 4 | 5 | 1 | 2
4 | 5 | 1 | 2 | 3
5 | 1 | 2 | 3 | 4

我仍然很难获得它,这实际上很简单,但是我无法解决它.到目前为止,我有以下代码:

I still have problems getting it, this is actually quite simple, but I have not been able to solve it. So far I have the following code:

<?php
echo "<table border='1'><br />";

for ($row = 0; $row < 5; $row ++) {
   echo "<tr>";

   for ($col = 1; $col <= 4; $col ++) {
        echo "<td>", ($col + ($row * 4)), "</td>";
   }

   echo "</tr>";
}

echo "</table>";
?>

但是,这只会生成以下内容:

However, this only generates the following:

1  | 2  | 3  | 4 
5  | 6  | 7  | 8
9  | 10 | 11 | 12
13 | 14 | 15 | 16
17 | 18 | 19 | 20

谢谢,任何帮助将不胜感激!

Thank you, any help would be appreciated!

推荐答案

<?php
echo "<table border='1'><br />";

for ($row = 0; $row < 5; $row ++) {
   echo "<tr>";

   for ($col = 0; $col < 5; $col ++) {
        echo "<td>", (($col + $row) % 5) + 1, "</td>";
   }

   echo "</tr>";
}

echo "</table>";
?>

这篇关于对于PHP中的循环表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆