$n=mysql_num_rows($rs);
    $i=0;    
    while($n>0)
        {
            while(($row=mysql_fetch_array($rs))&&$i<=5)
                {

                    echo $row['room_name'];
                    $i=$i+1;
                    //echo $n."<br>";
                }
                echo "<br>";
        //echo "n1=".$n;
        $n=$n-5;
        //
        $i=0;
        }

Output:101102103104105106
108109110

The row for roomname 107 is missing.... anybody please tell me what is the problem while reentering the loop again...

link|improve this question

73% accept rate
An example of the query that you are running might help – SeanJA Apr 7 '10 at 5:05
1  
Is this some sort of obfuscated PHP code contest? (I really hope so) – Chad Birch Apr 7 '10 at 5:05
1  
I notice the output is missing the <br>... I find that disturbing – SeanJA Apr 7 '10 at 5:07
The code-syntax police should lock you up son! – dscher Apr 7 '10 at 5:10
It has nothing to do with your English, this code is just extremely bad. It seems like you've jumped way ahead of your ability level, you're interacting with a database when you're missing fundamentals like for-loops. – Chad Birch Apr 7 '10 at 5:13
feedback

3 Answers

up vote 2 down vote accepted

When $i becomes 6 you fetch a row but do nothing. Because fetching happens before the $i<=5 check, the fetched row gets skipped.

Change the order of conditions in the while loop.

while(($row=mysql_fetch_array($rs))&&$i<=5)

To

while($i<=5 && ($row=mysql_fetch_array($rs)))
link|improve this answer
feedback

here if are checking $i<=5 condition so array stats from 0 , so your database values stats from 101,102,..106, so it will 6 elements .

$i<=5 condition remove this condition in while keep the condition if($i%5==0) echo "
"; it will works

link|improve this answer
feedback

Just to follow up on my comment, this whole chunk of code could have been written much more clearly as follows. (assuming you meant to put in a <br> after every 5 records, right now you're doing it after 6 but I think that's probably a mistake)

$rownum = 1;
while ($row = mysql_fetch_array($rs))
{
    echo $row['room_name'];

    if ($rownum % 5 == 0)
        echo '<br>';
    $rownum++;
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.