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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.