I want to only show records for today and later...

Discussion in 'PHP' started by alhen, Jun 20, 2007.

  1. #1
    Hey, I'm a musician trying to (simply) only show gigs that have not already passed. I thought I had a simple solution with this code:

    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $day=mysql_result($result,$i,"day");
    $date=mysql_result($result,$i,"date");
    $disp_date=mysql_result($result,$i,"disp_date");
    $timestart=mysql_result($result,$i,"timestart");
    $ampm=mysql_result($result,$i,"ampm");
    $location=mysql_result($result,$i,"location");
    $comments=mysql_result($result,$i,"comments");
    $en_day=mysql_result($result,$i,"en_day");
    $en_comments=mysql_result($result,$i,"en_comments");
    $today = date("Y-m-d");
    
    if ($date >= $today) { 
    echo "<div class=\"test\"><a href=\"en_gigs.php\"><b>$en_day</b>, <b>$date</b><br><b>$location</b></a></div>";
    } else {
    echo "<div class=\"test\"><b>No Performances Sceduled</b></div>";
    Code (markup):
    But the results aren't what I expected. I am testing with one gig on a date that has passed, and one in the future. With this code it displays "No performances scheduled". But if I change
    if ($date >= $today) {
    Code (markup):
    to
    if ($date <= $today) {
    Code (markup):
    ... using the less than sign, it WILL show the older gig. Any ideas?

    THIS IS MY EDIT: Just wanted to say, of course, my date format is in the same format by, YYYY-MM-DD... just incase that was your first question. =)

    Thanks, ~alhen
     
    alhen, Jun 20, 2007 IP
  2. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm sorry,
    But I just tried something that may shed more light on this...

    My code also limits to 1 entry... when I remove that limit, it shows "No performances scheduled" AND the future gigs.

    What's up with that?
     
    alhen, Jun 20, 2007 IP
  3. Travis

    Travis Peon

    Messages:
    539
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My question would be; shouldn't you be using microtime() instead of date()? I'm not sure if PHP can assume that a MM-DD-YYYY format can be greater or less than, etc. but if you do it all with seconds, calculate the second that will occur on a specific date, it would line up perfectly.
     
    Travis, Jun 20, 2007 IP
  4. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the reply Travis,
    Yea, PHP can sort by date as long as you have the date set appropriately.

    I did justy realize what I think my problem is. It is just going to the first gig entered. It can decipher if it's current or not, but even if it has expired it will use that entry and say, No performances scheduled. What I guess I need is a way to have it skip all of the expired dates and list the first one that is in the future.

    I'm still working on this, but if anyone has any ideas I'd love the help. I'll post if I figure it out.

    Thanks!
     
    alhen, Jun 20, 2007 IP
  5. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What's the datatype of the date field in your database?

    You can select the new gigs from the database directly by making some changes to your query. This will save you some time checking if the date is newer than the current one.

    I don't know if this is the correct syntax, but you can give it a try:
    $query = "SELECT *
    FROM
      my_table
    WHERE
      UNIX_TIMESTAMP(date) > ".time();
    PHP:
     
    UnrealEd, Jun 20, 2007 IP
  6. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    UnrealEd, THAT'S IT!! It was so simple. You rock!

    What I ended up with was:
    $todate = date("Y-m-d");
    
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM gigs WHERE date >='$todate' ORDER BY date ASC LIMIT 1";
    Code (markup):
     
    alhen, Jun 20, 2007 IP
  7. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Except... It will not show "No performances schedule" when there are no shows schedule... but I'm working on it now. I may have changed something earier that I have to go back and fix.

    if ($date >= $today) { 
    echo "<div class=\"test\"><a href=\"en_gigs.php\"><b>$en_day</b>, <b>$date</b><br><b>$location</b></a></div>";
    } else {
    echo "<div class=\"test\"><b>No Performances Sceduled</b></div>";
    }
    Code (markup):
     
    alhen, Jun 20, 2007 IP
  8. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use mysql_num_rows to see if there are any rows selected. If not, no new gigs are planned, and you can display the "No Performanced scheduled" message:
    if (mysql_num_rows($result) == 0) {
      echo "<div class=\"test\"><b>No Performances Sceduled</b></div>";
    } else {
      // loop over the results
    }
    PHP:
     
    UnrealEd, Jun 20, 2007 IP
  9. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Sorry, but like this?
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM gigs WHERE date >='$todate' ORDER BY date ASC LIMIT 1";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    $i=0;
    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $day=mysql_result($result,$i,"day");
    $date=mysql_result($result,$i,"date");
    $disp_date=mysql_result($result,$i,"disp_date");
    $timestart=mysql_result($result,$i,"timestart");
    $ampm=mysql_result($result,$i,"ampm");
    $location=mysql_result($result,$i,"location");
    $comments=mysql_result($result,$i,"comments");
    $en_day=mysql_result($result,$i,"en_day");
    $en_comments=mysql_result($result,$i,"en_comments");
    $today = date("Y-m-d");
    
    if ($date >= $today) { 
    echo "<div class=\"test\"><a href=\"en_gigs.php\"><b>$en_day</b>, <b>$date</b><br><b>$location</b></a></div>";
    } else if (mysql_num_rows($result) == 0) {
    echo "<div class=\"test\"><b>No Performances Sceduled</b></div>";
    }
    $i++;
    }
    Code (markup):
     
    alhen, Jun 20, 2007 IP
  10. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I also tried this:

    if (mysql_num_rows($result) == 0) {
    echo "<div class=\"test\"><b>No Performances Sceduled</b></div>";
    } else  {
    echo "<div class=\"test\"><a href=\"en_gigs.php\"><b>$en_day</b>, <b>$date</b><br><b>$location</b></a></div>";
    }
    Code (markup):
     
    alhen, Jun 20, 2007 IP
  11. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    you have to combine those two code snippets, like this:
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM gigs WHERE date >='$todate' ORDER BY date ASC LIMIT 1";
    $result=mysql_query($query) or die(mysql_error());
    
    if (mysql_num_rows($result) == 0) {
      echo "<div class=\"test\"><b>No Performances Sceduled</b></div>";
    } else {
      // this is an easier way to loop ove the returned results
      /* 
      * The $row variable is an array with the names of the database fields as keys. For instance you have a field named "date", 
      * then you can access the value using: $row['date']
      */
      while ($row = mysql_fetch_assoc($result)) {
        echo "<div class=\"test\"><a href=\"en_gigs.php\"><b>" . $row['en_day'] . "</b>, <b>" . $row['date'] . "</b><br><b>" . $row['location'] . "</b></a></div>";
      }
    }
    PHP:
    I think this should do the trick, haven't tested it though :)
     
    UnrealEd, Jun 20, 2007 IP
  12. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks UnrealEd,
    I really appreciate your help... I am really having a hard time understanding loops. I've googled around but haven't found any great tutorials yet, that start at the beginning but move on to things like this and beyond. Do you know of a good place(s) for an idiot like myself?

    Thanks again. You make it look so easy!

    ~alhen
     
    alhen, Jun 21, 2007 IP
  13. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Anytime man, i'm glad i could help :)

    Nobody's an idiot, we all have to start somewhere. When i started i even had problems with the most basic stuff :)

    When learning a language i really enjoy learning it from a book. My first php&mysql book was something like "php in 24 hours", those really help you to understand the basics. Then i bought myself the PHP5 & MySQL Bible, it was enough back then, but now it's too basic nowadays :). I recently bought the PHP Cookbook (version 3) from O'Reilly, which is a really good book, lot's of great scripts in there, and a pleasure to read.

    When it comes to websites: www.php.net, that's the best way to learn about the use of functions. Don't use it to learn the basics, or learn how to program, cause they don't help you with that
     
    UnrealEd, Jun 21, 2007 IP