Using PHP to format SQL results

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
stragglerat
Forum Newbie
Posts: 7
Joined: Wed May 14, 2008 12:00 am

Using PHP to format SQL results

Post 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!";
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Using PHP to format SQL results

Post 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. =]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
stragglerat
Forum Newbie
Posts: 7
Joined: Wed May 14, 2008 12:00 am

Re: Using PHP to format SQL results

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