
May 22nd, 2004, 06:00 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Finland
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Problem with table creating loop for MySQL results
My target is this: retrieve from the database LIMIT 10 images and show the images in HTML-table, where is 5 columns and two rows. If there are not 10 images, showing default image in the table after the images, which found.
My code works fine if images found in the table 1-4 items. But if images found 5 or more, the HTML-table does not work. Columns are only four and images after fourth are printed to the one column, and default images go to to the bottom of the table ugly.
I have tried to solve this multiple times, but I have had not success so far.I think the bug must be quite small thing in my while or for loop, but I cannot find out. Could someone find the solution, I'd be more than happy.
PHP Code:
<?php require_once "conn.php"; /* If for instance 7 images, then showing default image three times after the images that found (this is my target) */ $LIMITED=10; //Limit value $cols = 5;//setting number of columns for the table $j = 1; //for columns of the table $i = 0; //$i is for default image in the loop that calculates how many times default image is shown $result = mysql_query ("SELECT id,thumb_image FROM imagetable LIMIT $LIMITED"); $sum = mysql_num_rows($result); //how many results //works fine if < 5 images found, but 5 or more does not echo "<table border=0 cellpadding=4 cellspacing=4>"; echo "<tr>"; while ($row = mysql_fetch_array($result)) { if (is_int($j / $cols)){ echo "\n</tr><tr><td>"; echo "<a href=show_img.php?id={$row['id']} Target=_self>"; echo "<img src=pictures/".$row['thumb_image']." border=0></a>"; echo "</td></tr><tr>"; } else { echo "<td>"; echo "<a href=show_img.php?id={$row['id']} Target=_self>"; echo "<img src=pictures/".$row['thumb_image']." border=0></a>"; echo "</td>"; }//else ends $j++; } //while ends /* Calculating how many times default image must be shown */ for ($i = 0;$i < ($LIMITED - $sum);$i++){ if (is_int($i / $cols)) { echo "<td>"; echo "<img src=\"images/default.jpg\">"; echo "</td></tr><tr>"; } else { echo "<td>"; echo "<img src=\"images/default.jpg\">"; echo "</TD>"; } }//for ends echo "</tr>"; echo "</table>"; echo "</body>"; echo "</html>"; ?>
|