I need to create 3 HTML columns in PHP with data returned from MySQL. I would like the data split evenly between all 3 columns... How would I go about doing this?
解决方案
You could try doing something like this:
$result = mysql_query("SELECT value FROM table");
$i = 0;
echo '
while ($row = mysql_fetch_row($result)){
echo '
' . $row[0] . '';if ($i++ == 2) echo '
'}
echo '
';note this table has the values ordered like
1 2 3
4 5 6
7 8 9
If you wanted it vertically like
1 4 7
2 5 8
3 6 9
Then you should do something like
$result = mysql_query("SELECT value FROM table");
$data = Array();
while ($row = mysql_fetch_row($result)) $data[] = $row;
for ($i = 0; $i < count($data) / 3; $i++){
echo '
for ($j = 0; $j < 3; $j++){
echo '
' . $data[ $i + $j * 3] . '';}
echo '
'}
echo '
';