How do I display all my database information in an html table?



This example displays all of the thumbnail pictures in an html table. By changing one variable, you can have as many cells per row as you want.
NOTE: If you have a large number of images, you may want to modify this by adding PAGINATION to keep your screen more manageable


Now the actual scripting.

<?php
$max_columns 
5/* set max columns for table */
$i 0/* initially set counter to use in starting and ending rows */
?>
<table border ="2">
<?PHPinclude('db.php'); /* make connection to database here */
$query "Select * FROM album"/* define query */
$result mysql_query($query);   /* execute query */
$total_cells mysql_num_rows($result);  /* count total cells to be displayed */
$total_rows ceil($total_cells $max_columns);  /* calculate total rows needed */
$junk1 $total_rows $max_columns;  /* calculate number of empty cells in last row */
$junk2 $junk1 $total_cells;
if(
$junk2==0) { 
    
$last_row "</tr>"
}else{ 
    
$j 0
    while(
$j<$junk2){ 
        
$last_row $last_row "<td></td>"
        
$j ++; 
    } 
    
$last_row $last_row "</tr>"
}
while(
$row mysql_fetch_array($result)) {  /* begin looping thru the results */
    
$image "thumbs/" .  $row['image']; 
    if(
$i == 0){ 
        echo 
"<tr>";     
        
$i ++; 
    } 
    
?>
    <td align="center" valign="center"><IMG src = "<?PHP echo $image?>"></td>
    <?PHP 
    $i 
++;
    if(
$i $max_columns) {   /* check if need to close row */
        
echo "</tr>"
        
$i=0
    }        
}
echo 
$last_row "</table>"/* clean up last row */
?>

An example where we set the number of columns to 5.


Warning: mysql_connect(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/liteb/public_html/toh/db.php on line 6

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/liteb/public_html/toh/db.php on line 6

Warning: mysql_connect(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/liteb/public_html/toh/db.php on line 6

Warning: mysql_connect(): Access denied for user 'liteb_nick01'@'localhost' (using password: YES) in /home/liteb/public_html/toh/db.php on line 6
could not connect