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> </td>
<td>
<a href="<?php echo $url; ?>"><?php echo $rows['firstname']; ?></a>
<br>
</td>
</tr>
<?php
}
?>
Thanks!
HELP PLEASE: Display record field in two column??
Moderator: General Moderators
-
mapperkids
- Forum Newbie
- Posts: 15
- Joined: Mon Feb 11, 2008 8:53 pm
Re: HELP PLEASE: Display record field in two column??
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.
The % in those conditional statements is the modulo operator. See http://snook.ca/archives/php/the_modulo_oper/
Code: Select all
<?php
...
$ctr=0;
while ($rows = mysql_fetch_array($result)) {
if($ctr % 2) echo "<tr>";
echo "<td> </td>";
echo "<td><a href='$url'>" . $rows['firstname'] . "</a></td>";
if(!$ctr % 2) echo "</tr>";
++$ctr;
}
...