Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP dsplay

Options
  • 23-07-2007 9:20pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    I currently don't have a script for this as I don't know how it will work so please forgive what appears to be 'a lack of effort' (its not).

    I have a table, lets call it myTable, and it contains numerous rows. The rows are sorted by date (dd/mm/yyyy). There will be numerous rows which have the same date. I want to create a loop that will display all the rows of myTable (possibly in a table) but will keep all the rows with the same date together and insert a <br /> every time the date changes. eg.

    01/01/01
    01/01/01
    01/01/01
    <br />
    02/01/01
    02/01/01
    etc.

    How would I go about dong this?

    Thanks,
    S.


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Once you figure out how to do this once, tis easy :)

    Basically the way I do it (roughly) is;
    [php]
    $curr_date = "";
    while($row = mysql_fetch_array($result)) {
    if($row != $curr_date) {
    echo "<br />";
    $curr_date = $row;
    }
    echo $row;
    }
    [/php]


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Thats how I thought it was done but had problems coding it and there was one thing I wasn't sure of:

    $row = mysql_fetch_array($result)

    Does this get the row execute the statment then move onto the next row? Kinda like i++?

    Thanks,
    S.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The mysql_fetch_array($query_result) function reads the next row from $query_result and returns it in the form of an associative array. When it has reached the last row, calling mysql_fetch_array() again will return 0, or false, which causes the loop to break.

    Have you any code at the moment that you've been working on?


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Seachmall wrote:
    $row = mysql_fetch_array($result)
    Does this get the row execute the statment then move onto the next row? Kinda like i++?

    It's similar......if you had an actual array, you could do

    for ($i=0;$i<count($result);$i++) {
    :
    // PROCESS $result[$i]
    }

    This requires that you know the length of the array in advance, but requests to database don't work like that (at least not by default....you could get and use the row result count, but I've never seen that done in practice)

    So you use the while loop (basically "while a result can be fetched from the database, do this with each result, then as you indicated (because it's a while loop) move on to the next row")

    Essentially, everything within the while {} statement is done for every row. Just bear in mind that everything is replaced each time, so in order to "remember" a value to compare it next time through, you need to explicitly store it; i.e. you can't do

    if ($date[$i]==$date[$i-1])

    because the record being used is replaced (in PHP, not in the DB) each time though the loop.

    That's why seamus' example includes the $curr_date variable - it "remembers" the date from the previous time through the loop, in order to be able to compare it.

    Oh yeah - I presume the database query is "ORDER BY [date_field_name]" ?


Advertisement