Re: Nested for loop problem
First off, always remember that you should do your best to query a database as few times as possible. This is key for maximum performace.
So, you really only want two queries in this instance.
- First query to get the category names. Store this in a PHP array.
- Second query to get all the images' data into a PHP array.
From there, you can read the first array to determine into what category to put this data. Personally, I'd use a multidimentional array with the image categories as the first dimension index. Then, the second dimension could be all the individual image data. Here's an example.
Code:
array(
"Headshot" => array(
array(id, name, number, url),
array(id, name, number, url)
),
"Landscape" => array(
array(id, name, number, url),
array(id, name, number, url)
),
"Strobist" => array(
array(id, name, number, url),
array(id, name, number, url)
)
)