Page 1 of 1
[SOLVED] Nested while
Posted: Mon Feb 21, 2005 8:26 pm
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..
Posted: Mon Feb 21, 2005 8:55 pm
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)) {
echo ' -> '.$file2.'<br>';
$handle3 = fopen('./'.$file.'/'.$file2);
while ($file3 = readdir($handle3)) {
echo ' -> '.$file3.'<br>';
}
}
}
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 .. )
Posted: Mon Feb 21, 2005 10:01 pm
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.
Posted: Wed Feb 23, 2005 8:04 pm
by S_henry
It looks like quite complicated. Now i still trying to find where the error is. Thanx..
Posted: Thu Feb 24, 2005 6:28 am
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

Posted: Thu Feb 24, 2005 7:39 am
by patrikG
why don't you post your queries, I'm sure a simple join could remove any PHP complications.
Posted: Thu Feb 24, 2005 7:35 pm
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..
[SOLVED] Nested while
Posted: Sun Mar 06, 2005 9:15 pm
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++)
{
...
while ($point1 = mysql_fetch_object($result1))
{
...
}
...
}
Anyway, thanx guys for all advices.