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
multi column table - query output
Moderator: General Moderators
Re: multi column table - query output
First I decided to put everything I need into an array:
Then I loop through it with a for loop: (I have a limit of 20 in my query)
Works great.
What can I change to make it better?
Thanks,
- D
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";
}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;
What can I change to make it better?
Thanks,
- D
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: multi column table - query output
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>';