Guru Needed :) Help with Events script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KarlBee
    Junior Member
    • Mar 2004
    • 9

    #1

    Guru Needed :) Help with Events script

    Hi there,

    I have this script:



    PHP:

    Code:
    <?php   
    $date = date("Ymd", time()+3600*7);  
    $NumEvents = 0;  
    $handle = opendir('../event_dates');  
    $inc_files = array();  
    while (($file = readdir($handle)) !== false) {  
        if ($file >= $date) {  
            $inc_files[] = $file;  
        }  
    }  
    
    $NumEvents = count($inc_files);  
    if ($NumEvents > 0) {  
        sort($inc_files);  
        for ($i = 0; $i < $NumEvents; $i++) {  
            include '../event_dates/' . $inc_files[$i];  
        }  
    
        echo $NumEvents . " events are scheduled";  
    }  
    else {  
        echo "<strong class='red'>Sorry, no events are planned at present</strong>";  
    }  
    
    if($NumEvents > 2){ 
           $html = "<p><a href='#' onclick='java script:ScrollWin.scroll('0'); return false;'>Back to top</a></p>"; 
           } 
    
           echo $html; 
    
    ?>

    now I need to use this script but only show 1 event and that event has to be the one nearest the current date (ie todays date 19/07/204 and the closest event would be nearest this date)

    The script works by calling flat text files with the .txt stripped away. The file names are the dates of the events. So an event for today (4th August 2004) would be named 20040804

    Someone did give me this:

    Code:
    <?php    
    $date = date("Ymd", time()+3600*7);   
    $NumEvents = 0;   
    $handle = opendir('../event_dates'); 
    $i=1;  
    
    while (($file = readdir($handle)) !== false) {    
      if($i==1)  
      {  
        $hfile = $file;  
        $i++;  
       }  
      else  if ($hfile<=$file) {    
            $hfile = $file;    
        }  
    }  
    
    if($i!=1)  
      include '../event_dates/' . $hfile; 
    else {   
        echo "<strong class='red'>Sorry, no events are planned at present</strong>";   
    } 
    ?>
    and although it does show just the one date it shows the wrong one. It shows tha entry last posted as against the entry clostest to todays date.

    Can anyone help?

    Thanks
    Karl
  • Eric
    Junior Member
    • Mar 2004
    • 22

    #2
    Hi,

    Have you thought of storing your event information in a MySQL database instead? The query language is very powerful and would let you easily select the nearest date or the last 5 days, or etc. etc.

    Eric

    Comment

    • mkraai
      Junior Member
      • Mar 2004
      • 21

      #3
      The problem is that you still need to read them all in and sort them first.

      PHP Code:
      <?php   
      $date 
      date("Ymd"time()+3600*7);  
      $NumEvents 0;  
      $handle opendir('../event_dates');  
      $inc_files = array();  
      while ((
      $file readdir($handle)) !== false) {  
          if (
      $file >= $date) {  
              
      $inc_files[] = $file;  
          }  
      }  

      $NumEvents count($inc_files);  
      if (
      $NumEvents 0) {  
          
      sort($inc_files);  
          include(
      '../event_dates/' $inc_files[1]);  
      }  
      else {  
          echo 
      "<strong class='red'>Sorry, no events are planned at present</strong>";  
      }  

      ?>
      “Make everything as simple as possible, but not simpler.”
      — Albert Einstein

      “A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.”
      — Antoine de Saint-Exupéry

      Comment

      • KarlBee
        Junior Member
        • Mar 2004
        • 9

        #4
        Many thanks for your help Gentlemen.

        I will try out the code.

        Again thank you

        Karl

        Comment

        Working...