SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    organising by day

    Hello

    I got a select query that outputs a bunch of results. All the results have a timestamp column.

    How can I group these results by days and wrap each day into a div?

    e.g

    <div>
    result 1 - day1
    result 2 - day1
    result 3 - day1
    </div>

    <div>
    result 4 - day2
    result 5 - day2
    </div>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Order them by the timestamp column in your query. Then loop through the result set in PHP and create your output.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I have already ordered them by timestamp and created my output. Currently my output is this:

    result 1 - day1
    result 2 - day1
    result 3 - day1
    result 4 - day2
    result 5 - day2

    But I want to groups the days into divs like this:

    <div>
    result 1 - day1
    result 2 - day1
    result 3 - day1
    </div>

    <div>
    result 4 - day2
    result 5 - day2
    </div>

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Could you post your code please?

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, it is a very basic while loop.

    $loop = mysql_query("SELECT * FROM info");

    while ( $row = mysql_fetch_array($loop) {
    echo $row['result'];
    }

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  7. #7
    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)
    Show us what the column names are (rather than *) and how you are formatting your timestamp to show "day1".

  8. #8
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello

    This is my timestamp format: 2012-02-05 12:12:58

    this is my query, with the column name.

    $loop = mysql_query("SELECT result FROM info");

    while ( $row = mysql_fetch_array($loop) {
    echo $row['result'];
    }

  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)
    So how are you currently turning this:

    echo $row['result'];

    into lines like:

    result 1 - day1

  10. #10
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I used that as just an example.

    I have a one colum that needs to be outputted. The date wont be outputted.
    I want to wrap all the divs that come under one day into a div.

    So for example,

    if
    result 1
    result 2
    result 3
    are on 11th may 2011

    and
    result 4
    result 5
    result 6 are on 13th may 2011

    the result would be:

    <div>
    result 1
    result 2
    result 3
    </div>

    <div>
    result 4
    result 5
    result 6
    </div>

  11. #11
    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)
    Well if you want PHP to be able to format the results in terms of days you need to collect both the timestamp and the result so show us the output of putting this sql statement into your database management tool (PhpMyAdmin or whatever you use) where info is the name of your table.

    SHOW CREATE TABLE info

    or

    DESCRIBE info

  12. #12
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello

    my timestamp is stored in a 'timestamp' column. So the query would be..

    $loop = mysql_query("SELECT result, timestamp FROM info");

    while ( $row = mysql_fetch_array($loop) {
    echo $row['result'];
    }

  13. #13
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Pseudocoding it.

    $curdate = ''
    if num_rows > 0 {
    echo <div>
    while row = fetch_array {
    $date = date('dmY',result)
    if ($date != $curdate && $curdate != '') { echo </div><div> }
    $curdate = $date;
    }
    echo </div>
    }
    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
  •