SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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 Code:
     <?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)); ?>
    Last edited by guido2004; Feb 16, 2012 at 03:16. Reason: added php tags

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    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?

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    So the logic should be like this?

    Code:
    > 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?

  5. #5
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What comes out when you do:
    PHP Code:
    var_dump($row_Recordset1); 

  7. #7
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  8. #8
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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!

  10. #10
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fatso84 View Post
    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?
    Quote Originally Posted by Cups View Post
    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?
    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.

    Quote Originally Posted by Cups View Post
    Off Topic:

    @devbanana -- w00t! really glad to see you back posting on the PHP forum, we've missed you!
    Aww, thanks. I'm glad to be back myself.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    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
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •