SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While loop, problem using logic on last row?

    I have a while loop. In the loop is a date variable. What I am trying to do is compare the date from the current while iteration, to the date in the previous iteration. If this value is not the same, I want to output a summary row in my table.

    What I have works until you get to the very last row in the table. After the very last row I also want to display a summary for that date range. However, because the while loop stops after the last row I cant find a way to esentially make the code execute again creating this summary row?

    Example, I have three dates. June 1, June 8, June 15.


    June 1 - Data Field 1 - Data Field 2
    June 1 - Data Field 1 - Data Field 2
    June 1 - Data Field 1 - Data Field 2
    Summary for June 1
    June 8 - Data Field 1 - Data Field 2
    June 8 - Data Field 1 - Data Field 2
    Summary for June 8
    June 15 - Data Field 1 - Data Field 2
    June 15 - Data Field 1 - Data Field 2
    Summary field will not display!!!


    Here is the code I have.

    [code]
    while($row = mssql_fetch_array($result))
    {
    $datecur=$row['date'];

    echo "<tbody><tr>";
    if ($datecur != $dateprev) {
    echo "<td>SUMMARY</td></tr>"; }

    echo out table data here....

    $dateprev= $row['date'];
    }

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $row variable gets assigned the value of false on the last iteration. Just use a seperate variable to hold the value.
    PHP Code:
    while($row mssql_fetch_array($result))
    {
        
    $var_that_wont_get_overwritten_on_last_iteration $row;
    }

    var_dump($var_that_wont_get_overwritten_on_last_iteration); 


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
  •