You leave the inner loop when you’ve come to the end of the result set. And you leave the outer loop when you’ve come to the end of that same result set. So once you leave the inner loop, you also leave the outer loop.
I have a table where I enter exam results of different subjects for all students and want to generate a report, but its only displaying results for one student only using my above snippet.
The report outline should come out more like as follows;
John Smith
##############################
Business Law 86%
Quantitative Accounting 75%
Financial Mathematics 81%
Calculus II 90%
#############################
Mike Andrews
##############################
Business Law 72%
Quantitative Accounting 81%
Financial Mathematics 88%
Calculus II 73%
#############################
Paul Richards
##############################
Business Law 77%
Quantitative Accounting 79%
Financial Mathematics 83%
Calculus II 89%
#############################,
but instead its just printing out for John Smith only
I actually wonder whether he put the var_dump after the last call to fetch the row of data after his do…while. Otherwise he wouldn’t have gotten any data at all.
@fatso84: Try only calling mysql_fetch_assoc() once, assigning it to your variable, then using var_dump to get its output.
Not to mention that the symptom of it only showing up once is because you’re using a do…while instead of a while. (do…while will always run once - before the expression is evaluated. Which means your first runthrough of the loop, $row_Recordset1 should have no value, because it hasnt been filled yet). Turn it into a while, because you want the expression to evaluate before the execution of the loop.
var_dump $Recordset1. If you get bool(false) again, it means your query has screwed up and returned FALSE.
The whole thing can be done by turning the loop inside-out.
foreach record
if(s_id != last_s_id) {
Output headers. (Special if: if last_s_id is not null, output extra seperator)
}
Output record information
Set last_s_id to s_id
endforeach