本文介绍了来自数据库的数据与foreach - 从上到下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库:

  id |名称
1 | aaa
2 | bbb
3 | ccc
.. | ...
250 | zz3

foreach($ datafromdb as $ value){
echo $ value-> name();

$ / code $ / pre



  aaa bbb ccc ... zz3 

如果

< table>

pre $
< tr>
foreach($ datafromdb as $ value){
echo'< td>'。 $ value-> name()。 < / TD>;
}
< / tr>
< / table>

显示:

  aaa 
bbb
ccc
...
zzz3

我怎样才能使表12×12?

  AAA AAA1 bbb2 
BBB aaa2 bbb2
... aaa3 bbb3
... ... ....
zzz zzz2 zzz4



首先从上到下(12x),然后是下一列,再从上到下(12x)等12x b

我已经显示这个值从左到右,从左到右,从左到右等,但我想从上到下,从上到下,从上到下等等。

解决方案

丹尼尔Cherrington的代码是好的,但只有PHP只是做:

 < PHP 
$ data = range(1,240);
$ tab = array();
$ max = 12; ($ i = 0; $ i< sizeof($ data); $ i ++){
$ tab [($ i%$ max)] [] = $ data [$ i];
;
}

echo< table border ='1'>;
foreach($ tab as $ line){
echo< tr>;
foreach($ line as $ row){
echo< td>。$ row。< / td>;
}
echo< / tr>;
}
echo< / table>;
?>


database:

id | name
1  | aaa
2  | bbb
3  | ccc
.. | ...
250| zz3

foreach ($datafromdb as $value){
  echo $value->name();
}

this show me:

aaa bbb  ccc  ... zz3

from left to right

if

<table>
 <tr>
    foreach ($datafromdb as $value){
      echo '<td>' . $value->name() . '</td>';
    }
  </tr>
</table>

show:

aaa
bbb
ccc
...
zzz3

how can i make table 12x12 ?

aaa aaa1 bbb2
bbb aaa2 bbb2
... aaa3 bbb3
... ...  ....
zzz zzz2 zzz4

first from top to down (12x), then next column and again from top to down (12x) etc 12x

i have show this value from left to right, left to right, left to right etc, but i would like from top to down, top to down, top to down etc

解决方案

Daniel Cherrington's code is good, but with only PHP just do:

<?php
$data = range(1,240);
$tab = array();
$max = 12;
for($i=0;$i<sizeof($data);$i++){
    $tab[($i % $max)][] = $data[$i];
}

echo "<table border='1'>";
foreach($tab as $line){
    echo "<tr>";
    foreach($line as $row){
        echo "<td>".$row."</td>";
    }
    echo "</tr>";
}
echo "</table>";
?>

这篇关于来自数据库的数据与foreach - 从上到下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:17