r/PHPhelp • u/JLeeSaxon • Jan 26 '26
Solved Trouble with nested foreach loops and SELECT queries
I have a table of categories
| index_id | category_text |
|---|---|
| 1 | First Category |
| 2 | Second Category |
| 3 | Third Category |
| 4 | Fourth Category |
I have another table of survey questions
| index_id | main_text | category_id |
|---|---|---|
| 1 | a | 1 |
| 2 | b | 1 |
| 3 | c | 1 |
| 4 | d | 2 |
| 5 | e | 2 |
| 6 | f | 2 |
My goal is to show all of the survey questions, but grouped by category (and to have the option to only show a certain category, if I need that in the future). But it's not working.
Under "First Category", I am correctly getting only "a, b, c" (and under "Third Category" and "Fourth Category" I am correctly getting nothing, but under "Second Category", I am getting "a, b, c, d, e, f" instead of "d, e, f".
Here's my PHP (edit: now corrected):
$categories_query = mysqli_query($Connection, 'SELECT category_id, category_text FROM categories ORDER BY index_id ASC');
if ($categories_query && mysqli_num_rows($categories_query) != 0) {
while ($temp1 = mysqli_fetch_assoc($categories_query)) {
$categories_array[] = $temp1;
}
foreach ($categories_array as $Categories) {
echo '<h2>'.$Categories['category_text'].'</h2>';
echo '<ol id="'.$Categories['category_text'].'">';
$questions_query = mysqli_query($Connection, 'SELECT main_text FROM questions WHERE category_id = "'.$Categories['category_id'].'" ORDER BY index_id ASC');
if ($questions_query && mysqli_num_rows($questions_query) != 0) {
while ($temp2 = mysqli_fetch_assoc($questions_query)) {
$questions_array[] = $temp2;
}
foreach ($questions_array as $Questions) {
echo '<li>'.$Questions['main_text'].'</li>';
}
}
echo '</ol>';
unset($questions_array); // this is what I was missing, for anyone who comes across this
}
}
4
Upvotes
3
u/equilni Jan 27 '26 edited Jan 27 '26
Learn about joins, the loop over the data:
Results in:
7
u/Big-Dragonfly-3700 Jan 27 '26
The simple way of doing this is to use one appropriate type of JOIN query. Also simply index/pivot the data when you fetch it using the category id as the main array index (if you were using the PDO extension, it has a fetch mode that will do this for you.) You can then simply use two nested foreacch(){} loops to produce the output.
You would include or exclude categories by having a WHERE ... term in the query to match the categories you want.
1
u/flyingron Jan 26 '26
You keep appending to $questions_array in the outer foreach loop.
Put an unset right before that inner while loop (or otherwise empty $questions_array).
1
u/Timely-Tale4769 Jan 27 '26
Here, you are making three queries against the table, this is not preferable (until you are having more data), you can do that category logic using array_column or array_filter like functions.
1
u/BazuzuDear Jan 27 '26
Another way is to fetch the all the questions per category concatenated with GROUP_CONCAT, then iterate over the categories exploding each category's questions by the SEPARATOR you used in GROUP_CONCAT, that requires slightly less mapping/filtering/etc
blah blah
0
u/Wiikend Jan 26 '26
You're never unsetting the $temp2 array, so the values from the first iteration of the outer foreach bleed into the next iteration.