Andy Bas Posted July 11, 2009 Share Posted July 11, 2009 Hi Guys, I'm trying to format the results from a query into a table to have a number of images in a row... You know like google images... My issue is that there is something wrong with my logic along the way... and the first two rows are missing images (it's a bit odd as the rest seem to be ok, and then I tidy up at the end... Here is the code, and if you can spot my mistake(s) then there is a seat on the right hand of the 'Great Code Man' $num_records = mysql_num_rows($result); //Total number of records in result $num_per_row = 3; //Change to however many columns per row you desire $percentage = 100/$num_per_row; //for table formatting later on td width echo "<table width=\"450\" border=\"0\"> <tr>"; mysql_close(); $i = 0; while ($row = mysql_fetch_array($result)) { $ID=mysql_result($result,$i,"ID"); $ImageURL=mysql_result($result,$i,"ImageURL"); $sold=mysql_result($result,$i,"Sold"); if (($i == 2) || (($i > $num_per_row) && (($i % $num_per_row) == 1))) { echo "</tr><tr>"; } echo "<td width=\"$percentage%\"><a href=\"image.php?ID=$ID\"><img src=\"/pictures/thumbnails/$ImageURL\" border=\"1\"></a>"; if ($sold == 1) { echo "<img src=\"/images/sold.gif\" height=\"20\">"; } echo "</td> "; if (($i == $num_records) || (($i >= $num_per_row) && (($i % $num_per_row) == 0))) { //Fill in empty columns as necessary if (($i == $num_records) && ($i % $num_per_row != 0)) { $n = $num_per_row - ($i % $num_per_row); echo '<td colspan="' . $n . '"> </td>'; } echo "</tr>"; } $i ++; //Increment counter } I know it's a bit of a mess, but that's due to me messing with it (as it didn't work) and now it 'kind of works' but I get: With three columns 2 2 3 3 3 3 3 remainder With two columns 2 1 2 2 2 2 remainder It's all a bit odd if you ask me... But if I know how to fix it I would have by now (and I'm loathed to start from scratch as I'd just get a little confused again) Hope that you can help me (I'm sure that it's a simple mistake somewhere) A. Quote Link to comment https://forums.phpfreaks.com/topic/165604-solved-format-results-into-a-table-issues/ Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 I know it's a bit of a mess, but that's due to me messing with it Great code. Is readable code. Clean it up, not will you only find your own errors also others will find your errors faster. Quote Link to comment https://forums.phpfreaks.com/topic/165604-solved-format-results-into-a-table-issues/#findComment-873489 Share on other sites More sharing options...
kemo Posted July 11, 2009 Share Posted July 11, 2009 Well, I'm not sure if it has some effect on it, but why do you have mysql_close(); before you fetch some rows? I think it is atleast good practice to put this code at the end if it doesnt take effect in this case. You never know if you ever will need to something with database in while loop.. its only my opinion.. Quote Link to comment https://forums.phpfreaks.com/topic/165604-solved-format-results-into-a-table-issues/#findComment-873495 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 Have a look at this code FAQ to display results in mulitple columns. Quote Link to comment https://forums.phpfreaks.com/topic/165604-solved-format-results-into-a-table-issues/#findComment-873594 Share on other sites More sharing options...
Andy Bas Posted July 13, 2009 Author Share Posted July 13, 2009 Oh, that tutorial was smashing, a much quicker and simpler way than I was 'mashing' at. Thanks for your help everyone. Quote Link to comment https://forums.phpfreaks.com/topic/165604-solved-format-results-into-a-table-issues/#findComment-874498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.