Page 1 of 1

HELP PLEASE: Display record field in two column??

Posted: Tue Feb 17, 2009 12:42 pm
by mapperkids
Hi,

I have a problem, in my table there is a field called 'firstname' and I use the follow code to loop through the table and display the value of the field on a page and everything looks good, but now i want to display the field in two column format instead of one, I don't know how to do it.

Do I need two different loop/ do while?

I want the page display as follow two columns instead of one column, each value is from different record of the table.

===========================================
Firstname1 Firstname 2
Firstname3 Firstname 4
Firstname5 Firstname 6
===========================================




$result = mysql_query($sql) or die(mysql_error()."<br>$sql");

while ($rows = mysql_fetch_array($result))
{

<tr>
<td>&nbsp;</td>
<td>
<a href="<?php echo $url; ?>"><?php echo $rows['firstname']; ?></a>
<br>
</td>
</tr>

<?php

}

?>



Thanks!

Re: HELP PLEASE: Display record field in two column??

Posted: Tue Feb 17, 2009 1:28 pm
by califdon
A straightforward way to do that is to increment a counter each time you fetch another row, then check whether the counter is odd or even (evenly divisible by 2) to determine whether or not to start a new row or just add another item on the same row.

Code: Select all

<?php
...
$ctr=0;
while ($rows = mysql_fetch_array($result)) {
   if($ctr % 2) echo "<tr>";
   echo "<td>&nbsp;</td>";
   echo "<td><a href='$url'>" . $rows['firstname'] . "</a></td>";
   if(!$ctr % 2) echo "</tr>";
   ++$ctr;
}
...
The % in those conditional statements is the modulo operator. See http://snook.ca/archives/php/the_modulo_oper/