newbtophp Posted August 29, 2010 Share Posted August 29, 2010 I want to display 50 results, theirfore I'm using a while loop to do so, the issue is, if $row consists of results lower then 50 (it will display them) and not display 50..so I'm trying to figure out a way so even if $row doesn't cosist of 50 i'll display what it has aswell as continue the $i (and for the rest display NO CONTENT). I've come up with the following on the spot (not sure if it even would work) - but was wanting a better solution. $i = 0; $results = mysql_num_rows($result); while ($row = mysql_fetch_assoc($result)) { $i++; echo $i .' CONTENT '.$row['name'].'<br />'; } if ($result < 50) { for($i <= 50 - $result; $i++) { echo $i .' NO CONTENT<br />'; } } Link to comment https://forums.phpfreaks.com/topic/212019-looping-even-if-no-content/ Share on other sites More sharing options...
wildteen88 Posted August 29, 2010 Share Posted August 29, 2010 Not quite $i = 0; while ($row = mysql_fetch_assoc($result)) { echo ++$i .' CONTENT '.$row['name'].'<br />'; } $max_rows = 50; $num_rows = mysql_num_rows($result); if ($num_rows < 50) { for($i = $num_rows; $i < $max_rows; $i++) { echo $i .' NO CONTENT<br />'; } } Link to comment https://forums.phpfreaks.com/topic/212019-looping-even-if-no-content/#findComment-1104936 Share on other sites More sharing options...
newbtophp Posted August 29, 2010 Author Share Posted August 29, 2010 cheeers wildteen Link to comment https://forums.phpfreaks.com/topic/212019-looping-even-if-no-content/#findComment-1104939 Share on other sites More sharing options...
DavidAM Posted August 29, 2010 Share Posted August 29, 2010 or save a couple of steps with: $i = 0; while ($row = mysql_fetch_assoc($result)) { echo ++$i .' CONTENT '.$row['name'].'<br />'; } // At this point, $i contains the number of rows already output while ($i < 50) { echo ++$i .' NO CONTENT<br />'; } Link to comment https://forums.phpfreaks.com/topic/212019-looping-even-if-no-content/#findComment-1104952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.