multi column table - query output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cybercog
Forum Newbie
Posts: 20
Joined: Sat Oct 06, 2007 10:35 pm

multi column table - query output

Post by cybercog »

I'm trying to figure out the easiest way to return query results in two columns, but the data reads down column left, then down column right.

IE: if I have 20 names returned, I'd like to have names 1-10 on the left, then names 11-20 on the right.

I can come up with a way to do it, but I was wondering if there is some standard way of doing I don't know about.

Thanks,

- D
cybercog
Forum Newbie
Posts: 20
Joined: Sat Oct 06, 2007 10:35 pm

Re: multi column table - query output

Post by cybercog »

First I decided to put everything I need into an array:

Code: Select all

while( $rows = mysql_fetch_assoc( $results ) ) {
 
            $lc_id = $rows['lc_id'];
            $lc_f_name = $rows['lc_f_name'];
            $lc_l_name = $rows['lc_l_name'];
 
            $name = "$lc_l_name, $lc_f_name";
 
            $rec_array[] = "$lc_id|$name";
 
        }
Then I loop through it with a for loop: (I have a limit of 20 in my query)

Code: Select all

for($i=0;$i<=9;$i++) {
            $a = $i + 10;
 
            list($r_id, $r_name) = explode('|', $rec_array[$a]); 
            list($l_id, $l_name) = explode('|', $rec_array[$i]);
 
           $tr_td .= <<<HTML
<tr>
<td>$l_id</td><td>$l_name</td>
<td>$r_id</td><td>$r_name</td>
</tr>
HTML;
 
Works great.

What can I change to make it better?

Thanks,

- D
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: multi column table - query output

Post by jayshields »

If I understand you correctly, use of the mod operator (%) could be of some assistance.

Code: Select all

 
$counter = 0;
echo '<table>';
while($row = mysql_fetch_assoc($result))
{
  if(counter % 2 == 0) echo '<tr>';
  echo '<td>'.$row['whatever'].'</td>';
  $counter++;
  if(counter % 2 == 0) echo '</tr>';
}
echo '</table>';
 
Post Reply