繁体   English   中英

在数据库中创建带有动态数据的表

[英]Creating a table with dynamic data in Database

我有一个具有字段(行和列)的数据库。 我现在正在创建一个动态表。 这是我的代码。

    echo "<table border='1'>";
    $i = 1;
    while($i <= $fetch['row']) {
        echo "<tr>";
            $j = 1;
            while($j <= $fetch['col']) {
                echo "<td>";
                        echo "col ".$i." "."row".$j;
                echo "</td>";
                $j++;
            }

        echo "</tr>";
        $i++;
    }
    echo "</table>";

效果很好,结果就是这样。

-----------------------------------------------------------------------
| col 1 row 1 | col 1 row 2 | col 1 row 3 | col 1 row 4 | col 1 row 5 |
-----------------------------------------------------------------------
| col 2 row 1 | col 2 row 2 | col 2 row 3 | col 2 row 4 | col 2 row 5 |
-----------------------------------------------------------------------
| col 3 row 1 | col 3 row 2 | col 3 row 3 | col 3 row 4 | col 3 row 5 |
-----------------------------------------------------------------------
| col 4 row 1 | col 4 row 2 | col 4 row 3 | col 4 row 4 | col 4 row 5 |
-----------------------------------------------------------------------
| col 5 row 1 | col 5 row 2 | col 5 row 3 | col 5 row 4 | col 5 row 5 |

我该怎么做?

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

这是主意。 尚未运行代码。 根据输出表,$ i是列,$ j是行,这与代码中的内容不同。

echo ( ($i-1)*count($fetch['col']) ) + $j;  
// will have to switch $i, $j, $fetch['row'] if you want the output in the other order

($ i-1)* count($ fetch ['row'])是行偏移量的计算

第1行:(1-1)* 5 => 0 + $ j

第2行:(2-1)* 5 => 5 + $ j

 $i = 1;
 echo '<table border="1">';
 echo '<tr>';
 for($i; $i<=25; $i++){
   echo '<td>';
   echo $i;
   echo '</td>';
   if($i%5 == 0){
     echo '</tr><tr>';
   }
 }
 echo '</tr>';
 echo '</table>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM