I'm having problems executing a "do while loop" with two instances on one query

I’m having problems executing a “do while loop” with two instances on one query. Its running only one instance, my snippet is as follows;

 <?php do { ?>
<table width="100%" border="1" cellspacing="0" bordercolor="#000000">
          <!--DWLayoutTable-->
          <tr>
            <td>LEADERSHIP AND MANAGEMENT ACADEMY </td>
        </tr>
          <tr>
            <td>CANDIDATES' EXAMINATION RESULTS</td>
          </tr>
          <tr>
            <td height="21" colspan="5" valign="top"><div align="center" class="style24">PROGRAMME: </div></td>
        </tr>
          <tr>
            <td height="21" colspan="5" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
        </tr>
          <tr>
            <td><?php echo $row_Recordset1['ssurname']; ?></td>
          <td width="195" bgcolor="#CCCCCC" class="style1"><div align="center"><?php echo $row_Recordset1['sname']; ?></div></td>
          <td width="193" bgcolor="#CCCCCC" class="style1"><div align="center"><?php echo $row_Recordset1['s_id']; ?> </div></td>
          <td width="192" bgcolor="#CCCCCC" class="style1"><div align="center"></div></td>
          <td width="197" bgcolor="#CCCCCC" class="style1"><div align="center"></div></td>
        </tr>
          <?php do { ?><tr>
            <td class="style1"><div align="left"><?php echo $row_Recordset1['code']; ?></div></td>
          <td><div align="center"><?php echo $row_Recordset1['mark']; ?></div></td>
          <td><div align="center"><?php echo $row_Recordset1['classification']; ?></div></td>
          <td><div align="center"><?php echo $row_Recordset1['year']; ?></div></td>
          <td><div align="left"></div></td>
        </tr><?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
          <tr>
            <td><div align="left" class="style1">AGGREGATE:<?php echo $row_Recordset1['mark']; ?></div></td>
          <td><div align="center">WEIGHTED AVERAGE: </div></td>
          <td><div align="center">DEGREE CLASS: </div></td>
          <td><div align="center">OVERALL:</div></td>
          <td><div align="left"></div></td>
        </tr>
          <tr>
            <td height="57" colspan="5" valign="top" bgcolor="#CCCCCC"><!--DWLayoutEmptyCell-->&nbsp;</td>
        </tr>
              </table><?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

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.

What is it you want to achieve with this code?

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.

So the logic should be like this?

> for each student (outer loop)
  > display header
  > for each vote of this student (inner loop)
    > display vote
  > display average vote etc.

How do you decide you’ve reached another student?
Can you give an example of the data you’re getting with your query?

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

What comes out when you do:


var_dump($row_Recordset1);

Why are you doing an inner loop, when it is the same result set? Also, what is your need for a do … while instead of a simply while loop?

Agreed on seeing the var_dump results. :slight_smile:

I just did that and it returned bool(false). could there be an alternative way of tackling this, I’m relatively new to php & mysql?

That seems to suggest something going wrong higher up in the code.

You’ll have to post code nearer the query.

Think about how can you prove :

a) that you have connected to your database correctly?
b) that there is any matching data in your database?

Off Topic:

@devbanana – w00t! really glad to see you back posting on the PHP forum, we’ve missed you!

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.

Aww, thanks. :smiley: I’m glad to be back myself.

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