DaveLinger Posted March 28, 2009 Share Posted March 28, 2009 So basically I'm writing a quick-and-dirty script to read all of the items from a database, and display them in a table. I think the easiest way to accomplish this is to display each result, then check if the result number plus one divided by 3 is a whole number (for a 3 column table), and if it is, echo </tr><tr>. So here's what I'm thinking (looping per result of course). I'm adding one to the result number so that 0 becomes 1, and so on. echo "<td>this is some database info</td>"; $newresultnumber = $resultnumber+1; if($newresultnumber ??? is a whole number ???){echo "</tr><tr>";} ^^loop So what can I do to make this work? Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/ Share on other sites More sharing options...
ratcateme Posted March 29, 2009 Share Posted March 29, 2009 use the Modulo (%) it will return the remainder of a division equation so 1%3 returns 1 because 1 is the remainder of 1/3 but it will return 0 on 3%3 or 6%3 so you can have a database query script like for($i = 1;$row = mysql_fetch_row($result);$i++){ echo "<td>{$row[1]}</td>"; if($i%3 == 0){ echo "</tr><tr>"; } } Scott. Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/#findComment-796021 Share on other sites More sharing options...
DaveLinger Posted March 29, 2009 Author Share Posted March 29, 2009 Thanks Scott. Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/#findComment-796022 Share on other sites More sharing options...
DaveLinger Posted March 29, 2009 Author Share Posted March 29, 2009 How would I make it so that if it's the last result, it does NOT do this? I can close the tr and the table outside of the loop, but it's no good if it opens a new tr that I'll just have to immediately close at the end. Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/#findComment-796023 Share on other sites More sharing options...
ratcateme Posted March 29, 2009 Share Posted March 29, 2009 umm if you have a var say $rowcount with the number of items to be displayed you could write the if like if($i%3 == 0 && $i != $rowcount){ Scott. Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/#findComment-796031 Share on other sites More sharing options...
DaveLinger Posted March 29, 2009 Author Share Posted March 29, 2009 Ah, very true. Thanks again Scott. Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/#findComment-796033 Share on other sites More sharing options...
DaveLinger Posted March 29, 2009 Author Share Posted March 29, 2009 My final code (for others' reference): $totalrows = mysql_num_rows($result); if($totalrows >0){ for($i = 1;$row = mysql_fetch_row($result);$i++){ echo "<td><a href=\"game.php?id={$row[0]}\"><img src=\"images/games/thumbs/{$row[0]}_t.png\" alt=\"{$row[2]}\"><br />{$row[2]}</a></td>"; if($i%3 == 0 && $i != $totalrows){echo "</tr><tr>";} if($i == $totalrows && $i%3 != 0){ $i1 = $i+1; if($i1%3 == 0){echo "<td> </td>";} $i2 = $i+2; if($i2%3 == 0){echo "<td> </td><td> </td>";} } } }else{ echo "<p>No results found!</p>"; } As you can see I made it add enough blank cells after the last row to even out the table. Link to comment https://forums.phpfreaks.com/topic/151562-solved-this-should-be-easy-checking-whether-a-number-is-whole/#findComment-796045 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.