2017-03-17 66 views
2

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

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 
+0

你能澄清你的问题? – JapanGuy

+1

如果你只是想为它们编号,你可以在'$ i = 1;'下面添加'$ number = 1;'然后在你的内部循环里改变'echo“col”。$ i。“”。“ $ j ++;'add'$ number ++;' – JapanGuy

+0

@JapanGuy谢谢先生...我怎么把颜色放在​​的具体数字上想要​​23号是背景颜色:红色我该怎么做? –

回答

0

这是主意。没有运行代码。 $ 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)*计数($取[ '行'])是行偏移计算

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

行2:(2-1)* 5 => 5 +附加$ J

+0

谢谢先生...我怎么把颜色放在​​的具体数字上我想要​​23号是背景色:红色我该怎么做? –

+0

你应该使用css作为 $ style =''; 如果(<条件为颜色>) $风格= “风格=” 背景色:红 “; 或 .red {背景色:红} – blokeish

0
$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>'; 
+0

谢谢先生...我如何把颜色的具体数​​例如我想要​​23号是背景颜色:红色我该怎么做? –

+0

你可以把类放在td的红色和不同类为其他颜色和适用于这些类的css。你也可以通过jquery来做到这一点。 –

+0

相关问题