Jump to content

looping even if no content


newbtophp

Recommended Posts

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

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 />';
}
}

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 />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.