SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    New Jersey
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One date per day

    I am writing an archive system that displays the titles and decks of all the articles in my database sorted by date.

    I am displaying the results with this code:

    PHP Code:
    do {

    echo 
    '<b>' $article_row[date_added] . '</b><br /><br />';
    echo 
    '<a href=\"http://www.mysite.com/article/' .$article_row[id]. '\">' .$article_row[title]. '</a>  ';
    echo 
    $article_row[deck] . '<br /><br />';
    }

    while (
    $article_rowmysql_fetch_array($article_result)); 
    This works fine, however every article has a date echoed above it. When multiple articles are published on the same day the date becomes repetitive.

    How can I code it so that, on days with multiple articles, the date is only displayed once ... before the titles and decks of the articles written on that day?

    Thanks Much.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $last_date '';
    do {
    if ( 
    $article_row['date_added'] != $last_date) {
      echo 
    '<b>' $article_row['date_added'] . '</b><br /><br />'
      
    $last_date =  $article_row['date_added'];

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Gainesville, Fl
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks sweatje!

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    New Jersey
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow ... that code worked great, and did exactly what I wanted it to do! Thank you so much.

    For my own edification: the code you posted is setting $last_date to null, and then, each iteration of the do while clause, sets the value of $last_date to the date of the article. Finally, if $last_date is differnet from the article date, print the date to the page ... is that right? I'm new to manipulating iterations.

    Thanks Again.

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jluciani
    the code you posted is setting $last_date to null,
    Actually to an empty string, as null is different, but really just to anything that will not match the first date.

    Quote Originally Posted by jluciani
    and then, each iteration of the do while clause, sets the value of $last_date to the date of the article. Finally, if $last_date is differnet from the article date, print the date to the page
    Actually, it does not set on every iteration, but only when a different between the date in this time through the loop as compared to the last iteration. When that matches, it outputs the new heading, and then saves the current value as the "last_date".

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    New Jersey
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I get it now. I find iterations very interesting ... I will certainly be studying them more in the future.

    Thanks Again for your time and help.


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
  •