<?php
    //extract info from the table sc_books
    $sql  = "SELECT book_id, book_name, book_image
    FROM sc_books 
    GROUP by book_id  
    ORDER BY book_name";
    $result = mysql_query($sql) or die('Error: The list of books failed. ' . mysql_error());

    if (mysql_num_rows($result) == 0) {
    echo "The list is empty";
    } else 
{
    //fill in the table:
    echo '<table width="700" border="0" cellspacing="1" cellpadding="2" align="center">';

    //set the number of columns:
    $columns = 4;
    // set the width of each  book's image in percentatge (%); here 25% each
    $column_with  = (int)(100/$columns);
    
    $i = 0;
    while ($row = mysql_fetch_assoc($result)) {
    if ($i % $columns == 0) {// the rest of the division of $i by $columns
    echo '<tr>';
    }        
    echo '<td width="' . $column_with . '%">' . '<a href="indexu.php?page=detail_booku&book=' . $row['book_id'] . '">' . '<img src="../images/scbooks/' . $row['book_image'] . '" border="0">' . 
    '<br>' . $row['book_name'] . '</a><br />' . '</td>';
    // end the row when the modulo[ii] = 3 ( $columns - 1)
    if ($i % $columns == $columns - 1) {
    //end the row 
    echo '</tr>';
    }    
    $i += 1;
    }
    // fill in the empty places
    if ($i % $columns != 0) {
    while ($i++ % $columns != 0) {
    echo '<td width="' . $column_with . '%">&nbsp;</td>';
    }    
    echo '</tr>';
    }    
    
    echo '</table>';
}
    ?>