Help Displaying DB Data in Table in PHP

  • JamesThomas
  • Born
  • Born
  • JamesThomas
  • Posts: 4

Post 3+ Months Ago

Hey,

I'm want to display data from my database in a 4x4 table in PHP.

Lets for now say I have a name variable setup called $name and I want to select name from my database and display it in the PHP in a table. My code is as follows:-

Code: [ Select ]
    <table width="965px">
                        <tr>
                            <?php
 
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("setup") or die(mysql_error());
 
$query = "SELECT * FROM users";
$recset = mysql_query($query) or die("Error message if query
fails");
if( mysql_num_rows($recset) < 0 )
{
// no records found for query
}
else
{
while($row = mysql_fetch_assoc($recset))
{
?>
                            <td>
                               <?php $name = $row['name']; ?>
                                <?php echo $name; ?>
                                <p>
                            </td>
<?php
                               
 }
}
 
?>
                        </tr>
                    </table>
  1.     <table width="965px">
  2.                         <tr>
  3.                             <?php
  4.  
  5. mysql_connect("localhost", "root", "") or die(mysql_error());
  6. mysql_select_db("setup") or die(mysql_error());
  7.  
  8. $query = "SELECT * FROM users";
  9. $recset = mysql_query($query) or die("Error message if query
  10. fails");
  11. if( mysql_num_rows($recset) < 0 )
  12. {
  13. // no records found for query
  14. }
  15. else
  16. {
  17. while($row = mysql_fetch_assoc($recset))
  18. {
  19. ?>
  20.                             <td>
  21.                                <?php $name = $row['name']; ?>
  22.                                 <?php echo $name; ?>
  23.                                 <p>
  24.                             </td>
  25. <?php
  26.                                
  27.  }
  28. }
  29.  
  30. ?>
  31.                         </tr>
  32.                     </table>


But what I want to do is have 4 records, then break down into a new table row. I understand at the moment that it is displaying a table with one table row and then as many columns as there are records in the database, as long as there is no less than 1 record returned. This currently works.

My problem is though, I want a table with 4 columns and 4 rows displaying information from the database in each of the cells. If there are more than 16 values then I want to be able to allow the data to be split over multiple pages, can someone help with this too please?

Thanks in advance!

James.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • cjxxi
  • Expert
  • Expert
  • cjxxi
  • Posts: 565
  • Loc: Fort Worth, TX

Post 3+ Months Ago

$100 bucks and I do it for you, Understand the economy is down, and we need some income sometimes in more complex small projects. Let me know if you are interested
  • JamesThomas
  • Born
  • Born
  • JamesThomas
  • Posts: 4

Post 3+ Months Ago

No thanks, I'm an economist ;)
I've sorted this out myself now. Didn't take too long.
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

hint: use a counter in the while loop.

like
Code: [ Select ]
 
$counter = 0;
$str = '<table>';
while($row = mysql_fetch_assoc($recset)){
if($counter%4 == 0)
$str .= '<tr>';
$str .= <<<EOT
<td>
$row['blah']
</td>
EOT;
if($counter%4 == 3)
  $str .= '</tr>';
++$counter;
}
$str .= '</table>';
echo $str;
 
  1.  
  2. $counter = 0;
  3. $str = '<table>';
  4. while($row = mysql_fetch_assoc($recset)){
  5. if($counter%4 == 0)
  6. $str .= '<tr>';
  7. $str .= <<<EOT
  8. <td>
  9. $row['blah']
  10. </td>
  11. EOT;
  12. if($counter%4 == 3)
  13.   $str .= '</tr>';
  14. ++$counter;
  15. }
  16. $str .= '</table>';
  17. echo $str;
  18.  

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 117 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.