while loop within foreach/while loop....
Posted: Tue Feb 03, 2009 6:35 am
Hi,
I've got some rows inside a mysql database table, I want to group them via a column called 'resource' and have that 'resource' displayed at the top of each group.
The 'resource' element is created from a user-input text field - I have no way of knowing what it will be, but some entries will belong to the same resource.
At the moment I have tried using two approaches;
The first called the mysql_fetch_array on the table twice, with one "SELECT DISTINCT resource FROM table" and then put those elements into an array ($res):
The other did the same as above but used two while loops... the first loop called each resource type and then the second would call the data....
If my explanation is a bit hard to understand, I want the code to have:
edit:
at the moment that code puts out
It is executing the first foreach statement then the entire while statement and then going back and finishing the the foreach statement without executing the while statement again...
I've got some rows inside a mysql database table, I want to group them via a column called 'resource' and have that 'resource' displayed at the top of each group.
The 'resource' element is created from a user-input text field - I have no way of knowing what it will be, but some entries will belong to the same resource.
At the moment I have tried using two approaches;
The first called the mysql_fetch_array on the table twice, with one "SELECT DISTINCT resource FROM table" and then put those elements into an array ($res):
Code: Select all
<?php
[color=#00BF00]foreach[/color] ($res as $l) {
[color=#0000FF]echo[/color] [color=#FF0000]"<tr><td>$l</td></tr>"[/color];
[color=#FFBF00]//list row data within resource type[/color]
[color=#00BF00]while[/color] ($r = [color=#0000FF]mysql_fetch_array[/color]($fin)) {
[color=#00BF00]if[/color] ($r[[color=#FF0000]'resource'[/color]] [color=#0000FF]==[/color] $l) {
[color=#0000FF]echo[/color] $r[[color=#FF0000]'blahblah'[/color]];
}
}
}
?>
If my explanation is a bit hard to understand, I want the code to have:
Code: Select all
Resource1:
>>list rows which belong to this resource>>
Resource2:
>>list rows...>
etcat the moment that code puts out
Code: Select all
Resource1:
>>rows which belong to resource1>>
Resource2:
Resource3:
Resource4:
etc