1

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 =
=========
5
  • possible duplicate of Render a Table the "wrong" way: transpose Columns and Rows? Commented Oct 11, 2013 at 1:33
  • No. It's a different thing. I want to display half of the rows in the first column and the other half in the second column. Commented Oct 11, 2013 at 1:54
  • Create an array of all the results. Then split it up into two arrays, and output $array1[$row] and $array2[$row] on each row. Commented Oct 11, 2013 at 1:58
  • Thanks, but with the code above how would I do it? Isn't the third line which is while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { already an array? If so, where would I insert $array1[$row]? Commented Oct 11, 2013 at 2:06
  • possible duplicate of PHP Sorting files in a table Commented Oct 14, 2013 at 15:55

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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";  

Comments

0

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>";

Comments

0

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;

?>

1 Comment

I think this prints the table in the same order as the original.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.