Page 1 of 1

Using PHP to format SQL results

Posted: Wed May 14, 2008 12:04 am
by stragglerat
First of all, I just picked up PHP and SQL out of necessity for a new site. What I've learned so far has amazed me. I'm having a little hiccup, though. I'm using PHP to execute queries to an item database and "echo"ing the html code to set the returns in a table. The method I'm using, though, is only allowing one cell per table row. I'd like to have the items displayed two-per-row. I've tried to manipulate the code a little, but have yet to find a solution. The example code below is returning the results in a single table row. Any ideas on how to drop in a </tr><tr> after every two results?

Code: Select all

if (mysql_num_rows($result) > 0) {
 
echo "<table border=0 cellspacing=0 cellpadding=0 class=box_width_cont product>";
echo "<tr>";
 
while($row = mysql_fetch_row($result)) {    
 
echo "<td>";
echo "<center><img height=125 width=175 src=".$row[1]."></center>";
echo "<center><font color=#855b63><b>".$row[2]."<br>".$row[3]."<br>".$row[7]." Cut<br>Size: ".$row[4]."</b><br><br></font><font color=#855b63 size=3><b>".$row[10]."</b></center></font><br>";
echo "<center><a href=items/".$row[0].".php><img src='images/button_details.gif'></a></center>";
echo "</td>";   
        
}   
echo "</tr>";
echo "</table>";
    
   
}
else {
    // no
    // print status message
    echo "No items found!";
}

Re: Using PHP to format SQL results

Posted: Wed May 14, 2008 12:23 am
by s.dot
a tr after every 2 results... ;d

Code: Select all

 
echo '<table>
<tr>';
 
$i = 0;
while (...)
{
    if ($i == 2)
    {
        echo '</tr><tr>';
        $i = 0;
    }
 
    //your normal table code here
 
    $i++;
}
 
echo '</tr>
</table>';
:) That should take care of it.

Please use the [ php ] and [ code ] BBCode tags when posting code in the forums. =]

Re: Using PHP to format SQL results

Posted: Wed May 14, 2008 12:39 am
by stragglerat
Dude, you're a genius :). I've been searching for an answer all night, and the simplest little count fixed it. Thanks, bro.