0
    $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...

5
  • An example of the query that you are running might help Commented Apr 7, 2010 at 5:05
  • 1
    Is this some sort of obfuscated PHP code contest? (I really hope so) Commented Apr 7, 2010 at 5:05
  • 1
    I notice the output is missing the <br>... I find that disturbing Commented Apr 7, 2010 at 5:07
  • The code-syntax police should lock you up son! Commented Apr 7, 2010 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. Commented Apr 7, 2010 at 5:13

3 Answers 3

2

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)))
Sign up to request clarification or add additional context in comments.

Comments

2

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++;
}

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.