SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying Rows by Month and Day

    I was discussing this on another thread, but I was never able to get it to work for some odd reason. I can display the entire database table, or I can display every row in a given month, but I can't display everything for a given date (month and day).

    So I wanted to make a fresh start, and see if it works this time.

    From the top, my database features columns named Date_Event (a DATE field), Event (the event that occurred on a given date), and Text2 (the second paragraph of an event's account, if any).

    Here are some typical entries under Date_Event:

    2002-01-28
    1972-01-28
    1968-02-28

    Notice that all three years are different, but the first two dates are the same - 01-28, or January 28. I would like to display all the events that occurred on January 28, regardless of year in a dynamic table that looks something like this:

    <table>
    <tr><td><strong>[Date_Event]</strong> </td><td>Event</td></tr>
    <tr><td colspan="2">Text2</td></tr>
    </table>

    Also, I would like to display only the year under Date_Event. So instead of seeing this...

    1888-01-28 The first gorilla was captured.

    [Text2]It was named Bingo.

    Visitors will see this:

    * * * * * * * * * *

    1888 The first gorilla was captured.

    It was named Bingo.

    1972 The gray wolf was placed on the Endangered Species List. The listing was opposed by ranchers.

    Wolves remain endangered around the world due to persecution and habitat loss.

    * * * * * * * * * *

    I don't know if there was some flaw with the other script that I couldn't track down, or if I was making some kind of mistake. So I'd like to start with a fresh script and see if I can get it right this time. I'll display the entire script, including database connection, on a page with NO other html to make it simpler.

    Any tips?

    Thanks.

  2. #2
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    P.S. The ultimate goal obviously isn't to display everything that happened on January 28 but to display everything that happened on the current date, whatever it is.

  3. #3
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How feasible/practical would it be for you to separate dates into three fields in your database - year, month, day? If you could do that, then it would be a very simple query to select records by month and day. Just a thought.

    I understand what you're saying about the current date, but still, having three separate date fields would more easily allow you to select records by a given day.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    How feasible/practical would it be for you to separate dates into three fields in your database - year, month, day? If you could do that, then it would be a very simple query to select records by month and day. Just a thought.

    I understand what you're saying about the current date, but still, having three separate date fields would more easily allow you to select records by a given day.
    Well, I suppose I could do that pretty easily. I wanted to learn how to manipulate date fields - Year - Month - Day - and I think that would be the most elegant and efficient method. But if I can't make it work, then I'll follow your suggestion and split Date_Event into three fields.

    Thanks.

  5. #5
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Back to old script

    Taking back from the old thread:
    PHP Code:
    <?php
    //[DATABASE CONNECTION]
    list($d$m) = explode("-"date("j-m")); // a m to get leading zero
    $sql "SELECT Date_Event, Event, Text2 FROM gzcalendar WHERE DAYOFMONTH(Date_Event) = $d AND MONTH(Date_Event) = $m";
    MONTH(Date_Event) = $m";
    $res = mysql_query($sql) or die('mysql_error() on '.$sql.' gives '.mysql_error());
    echo "
    <table>";
    while(
    $row = mysql_fetch_array($res))
    {
        
    $date_event_ex =  explode("-", $row['Date_Event']);
        echo "
    <tr><td><class='pflat'><strong>". $date_event_ex[0] ."</strong".$row['Event'] ."</p></td>";
        echo "
    <td>".$row['Text2'] ."</td>";
        echo "
    </tr>";
    }     
    echo "
    </table>";
    ?>
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but the last ?> is grayed out, indicating an error in the script. And thtis is the error message:

    parse error, unexpected '='

    ...on this line...

    PHP Code:
    MONTH(Date_Event) = $m"; 
    If I change = to is, I get an unexpected T_STRING on the same line.

    Thanks.

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the problem - I just removed that line entirely, and it now works perfectly!

    Thanks so much.

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    I found the problem - I just removed that line entirely, and it now works perfectly!
    hmmm surely the line needed to be in there for something!!

    the only thing wrong with that string was the " at the end is hould have been:

    PHP Code:
     MONTH(Date_Event) = $m
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  9. #9
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooops it's a bad copy & paste originated from your old thread http://www.sitepoint.com/forums/show...0&postcount=14
    I didn't see it.
    Anyway, you're perfectly right, throw out the line.
    But keep the " ending the string from the SQL request.

    MONTH() is no PHP function but SQL used as just before. Even when that was a function, what's the sense to affect some value to it's evaluation ?
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source


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
  •