Reputation: 21
Here's my code:
echo "<table><tr>";
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
$data = $row['info'];
echo "<td>".$data."</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
The above code works as:
=========
= 1 = 2 =
= 3 = 4 =
= 5 = 6 =
=========
But how do I display the info in this format?
=========
= 1 = 4 =
= 2 = 5 =
= 3 = 6 =
=========
Upvotes: 1
Views: 7180
Reputation: 5625
Here is some php magic that could shorten that code for a bit
<?php
$array = range(1, 20);
$count = 2;
$out = array_chunk($array, ceil(count($array)/$count));
array_unshift($out, null);
$out = call_user_func_array("array_map", $out);
?>
<table>
<?php foreach ($out as $row) : ?>
<tr>
<?php foreach($row as $column) : ?>
<td><?php echo( $column ); ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Also, i would recommend you switch your database-related code to PDO. Besides all those nice features, like auto-escaping, you can easily get an array of elements from your DB query, without all this mysql_fetch_array
nonsense.
Upvotes: 0
Reputation: 21
Thanks, guys! I appreciate all your help, but this code works perfect for me. I just want to share it with others who may find this helpful. :)
$tmp=array();
$columns=2;
$row=ceil(mysql_num_rows($result)/$columns);
for ($x =1; $x <= $columns; $x++)
for ($y = 1; $y <= $row; $y++)
$tmp[$x][$y]=mysql_fetch_array($result);
echo "<table align=center width=\"50%\">\n";
for ($y =1; $y <= $row; $y++) {
echo "<tr>";
for ($x = 1; $x <= $columns; $x++)
if (isset($tmp[$x][$y]['ID']))
echo "<td>".$tmp[$x][$y]['info']." </a></td>";
else
echo "<td></td>";
echo "</tr>\n";
}
echo "</table>\n";
Upvotes: 1
Reputation: 1378
Actually, you don't need to parse array to another form...
<?php
$a = array(1,2,3,4,5,6,7);
$c = ceil(count($a)/2);
echo "<table>";
for($i=0; $i<$c;++$i){
echo "<tr><td>{$a[$i]}</td><td>{$a[$i+$c]}</td></tr>";
}
echo "</table>";
?>
Ofcourse you need to modify this code by adding db operations (like mysql_num_rows instead of count) but it works fine: enter link description here
Upvotes: 1
Reputation: 225
This is untested, but you can acheive this cleanly with 2 loops. Build up the left and right columns based on the the $count
variable. Build the HTML all the way up, then echo it.
Edit: I didn't realize you were dividing the data in half and placing the first half on the left column of the table. I have changed the code below to do this.
<?php
$left = array();
$right = array();
$count = 1;
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($count <= $num_rows/2) {
array_push($left, $row['info']);
} else {
array_push($right, $row['info']);
}
$count++;
}
$html = "<table>";
for ($i = 0; $i < $count; $i++) {
$html .= "<tr><td>"
. htmlentities($left[$i])
. "</td><td>"
. htmlentities($right[$i])
. "</td></tr>";
}
$html .= "</table>";
echo $html;
?>
Upvotes: 0
Reputation: 31614
A brief PSA... the mysql_
extension is depreciated and will eventually be removed. You need to start using mysqli_
now.
You need to parse your data first. Once you've done that, generating the table is easy. This should work for all data sets
echo "<table>";
$count = 1;
$col1 = $col2 = array();
$rowcount = round(mysql_num_rows($result) / 2);
while($row = mysql_fetch_assoc($result)) {
if($count > $rowcount) $col2[] = $row['info'];
else $col1[] = $row['info'];
$count++;
}
$counter = 0; // Arrays start with 0
foreach($col1 as $row) { // $col1 will always be >= $col2
$row2 = (isset($col2[$counter])) ? $col2[$counter] : "";
echo "<tr><td>" . $row . "</td><td>" . $row2 . "</td></tr>";
$counter++;
}
echo "</table>";
Upvotes: 0