[SOLVED] Nested while

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

[SOLVED] Nested while

Post by S_henry »

In my system, i want to create a table to display list of students. For each students, i also want to display their marks together. For example, in first row in that table, it will display |John|85|92|... where 85 is his mark for subject A, 92 for subject B and so on. In my opinion, i've to use nested loop like this to do that.

Code: Select all

while ($point1 = mysql_fetch_object($result1))
{
   ...
   while ($point2 = mysql_fetch_object($result2))
   {
      ...
   }
   ...
}
First loop is to display list of Students (up-to-down direction) and second loop is to display list of Marks (left-to-right direction). The problem is it doesn't work as i expected. The first loop is ok because it can display all students but second loop got problem because just display marks for the first student only. So anybody got any idea? Feel free to ask me more if my explanation still not enough. Thanx..
Last edited by S_henry on Sun Mar 06, 2005 9:35 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The nesting looks fine to me.... are your queries right?... maybe u could post some actual code from the script with the problem?

To demonstrate the nesting is ok.

Let's say I have these folders & subfolders with files in:

folder1
-> subfolder1
-> sub1file1.txt
-> sub1file2.txt
folder2
-> subfolder2
-> sub2file1.php
-> sub2file2.php
-> sub2file3.php

Code: Select all

$handle1 = fopen('./');
while ($file = readdir($handle)) {
    echo $file.'<br>';
    $handle2 = fopen('./'.$file);
    while ($file2 = readdir($handle2)) &#123;
        echo '&nbsp;&nbsp;&nbsp; -> '.$file2.'<br>';
        $handle3 = fopen('./'.$file.'/'.$file2);
        while ($file3 = readdir($handle3)) &#123;
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -> '.$file3.'<br>';
        &#125;
    &#125;
&#125;
The code here would output the same way I showed the directory listing above... (obviously assuming that the directory structure is strictly as i said and without . and .. )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

be careful with performing a query based on another query in a loop.. you can quickly run through available memory for the database. Basically, free the results of the inner query after using it on each iteration of the outer query.
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

It looks like quite complicated. Now i still trying to find where the error is. Thanx..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

So you run a query on the database right?.... then I'm assuming you're doing what you need to and creating then running the second query from inside the first loop (the query will change for each loop it makes). If you are using a query defined outside the first loop then you'll get whats happening at the moment (since it'll be the same query used over and over).

Post your code and we'll help you out :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

why don't you post your queries, I'm sure a simple join could remove any PHP complications.
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

Correct. I did like d11wtq said. So i'll post my query but on next week because actually right now i'm still on medical leave (at home) and my code is in the office. So keep in touch. Thanx guys..
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

[SOLVED] Nested while

Post by S_henry »

OK. i've solved my problem already. Instead of using nested While, i've tried to use While loop inside For loop and its work.

Code: Select all

for ($i=1; $i<$x; $i++)
&#123;
   ...
   while ($point1 = mysql_fetch_object($result1))
   &#123;
      ...
   &#125;
   ...
&#125;
Anyway, thanx guys for all advices.
Post Reply