Displaying database data in a table - URGENT!!!

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
stonewater
Forum Newbie
Posts: 1
Joined: Tue Nov 15, 2005 12:32 am

Displaying database data in a table - URGENT!!!

Post by stonewater »

Hi

I am currently connecting to a mysql database and displaying the data in a table via php.

My problem is this: I need the data to show in a table 5 columns wide (ie. 5 records) and then continue on the next row in the same way.

This is what the html code looks like - it is running within a print command in a while statement.

Code: Select all

<table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
	<tr>
		<td height=\"25\">&nbsp;
		</td>
	</tr>
  <tr>
    <td valign=\"middle\" align=\"center\">
		<table width=\"90\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
               <tr>
                 <td class=\"thumb_border\"><img src=\"products/mags-thumbs/$code.jpg\" width=\"90\" height=\"92\"></td>
                    </tr>
					<tr>
                      <td class=\"text\">Code: $code</td>
                    </tr>
					<tr>
                      <td class=\"text\">Price: $price</td>
                    </tr>
					<tr>
                      <td class=\"text\">Sizes: $size1</td>
                   </tr>
         </table>
	</td>
  </tr>

</table>
Thanks, Jono

HawleyJR: Welcome to DevNet ~ Please use tags when Posting PHP Code In The Forums
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

eww tag soup .. I might suggest http://www/w3c.org tutorials. However..


In order to get a table layout of 5 columns wide and assuming a left to right "write" of the data..

Code: Select all

<html>
<table>
<tr>
<?php
//assume connection to database and query has been made, lets fetch results in a loop
$i = 1;
while($data = mysql_fetch_array({resource_id})
{
      if($i%5 == 0)
      {
            echo "</tr><tr>";
      }
     echo "<td>$data['field_name']</td>";
    $i++;
}
$remainder = $i%5;
while($i>0)
{
      echo "<td></td>";
      $i--;
}
?>
</tr>
</table>
</html>
Mind you that's more pseudo code written on the fly than anything tested, but that's pretty much one way to do it ..
Post Reply