HELP PLEASE: Display record field in two column??

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
mapperkids
Forum Newbie
Posts: 15
Joined: Mon Feb 11, 2008 8:53 pm

HELP PLEASE: Display record field in two column??

Post 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!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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/
Post Reply