how to display data in 2 columns

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
mozer
Forum Newbie
Posts: 2
Joined: Sun Sep 28, 2008 12:38 am

how to display data in 2 columns

Post by mozer »

i need help!!!
i need to display the following table in 2 columns instead of 1.

function displaySupConCompany()
{
$result = mysql_query("SELECT SupConCompany FROM `suppliercontacts` ORDER BY `suppliercontacts`.`SupConCompany` ASC");
print("<table border='1'>");
while ($i<=20 and $row = mysql_fetch_row($result))
{
print("<tr>");
foreach ($row as $col)
{
$i++;
print("<td><a href=\"suppliercontacts_ismail_muad.php?id=$id\">$col</a></td>");
}
print("</tr>");
}

thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to display data in 2 columns

Post by califdon »

Think about it this way: The reason it now prints in one column is that you are printing a <tr> tag before every record and a </tr> tag after every record. So instead, only do that before/after every second record. In other words, in both cases, use an if test and only print them if $i is evenly divisible by 2. You will also have to test at the end of your while loop and if the last value of $i was odd, add a final </tr> tag.

An easy way to test whether a value is evenly divisible by 2 is:

Code: Select all

if( ($i % 2) == 0 )
mozer
Forum Newbie
Posts: 2
Joined: Sun Sep 28, 2008 12:38 am

Re: how to display data in 2 columns

Post by mozer »

Thank you kindly!
:lol:
Post Reply