SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I get a PHP include to update automatically after a set date?

    Hi guys,

    I maintain a number of website for sports athletes and on their websites I have an include for their next event - just a small PHP file with when and where the next event is.

    After each event I have to manually update the include to the clients' next event which is a bit of a pain and quite time-consuming.

    Is there any way I can set up the include script to automatically update with a different file to include after a set date?

    Or, even better, can I set up the include itself to display the updated content after a specific date?

    This is what the include contains...

    Code:
    <img class="event" src="/images/eventPicture.png" />
    <p class="nextEvent">Location<br />
    Series<br />
    1-3 January 2013</p>
    Thank you very much and I hope to hear from you!

    SM

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Something like this would work:

    PHP Code:
    <img class="event" src="/images/eventPicture.png" />
    <p class="nextEvent">
    <?php
    $events 
    = array(
        
    // end date => description
        
    '2013-05-02' => 'Location<br />Series<br />1st of May 2013',
        
    '2013-06-02' => 'Location 2<br />Some series<br />1st of June 2013',
    );
    $now = new DateTime();
    $eventShown false;
    foreach(
    $events as $date => $event)
    {
        if (
    $new < new DateTime($date))
        {
            
    $eventShown true;
            echo 
    $event;
            break;
        }
    }
    if (!
    $eventShown)
    {
        echo 
    "No upcoming events";
    }
    ?>
    </p>
    This will show the first message up until the second of may (so it also shows on actual day of the event), and then switches to the next one. This way you can just load a bunch of events in the script and let the logic decide which one is shown at a particular point in time. After they are all done just load a new bunch, etc.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Scallio!
    Brilliant - that works, thank you so much!
    You've just saved me loads of time!
    Thank you very much again! - top shooter!
    SM

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Rémon,

    I used your code and today it should've updated but is still just displaying the first date in the list. Have I done something wrong?

    I edited the code you sent slightly but essentially it is the same. Here it is...

    Code:
    <?php
    $events = array(
        // end date => description
        '2013-04-22' => '<img class="circuit" src="sano.png" /><p class="next">Sano<br />Series<br />20-21 April 2013</p>',
        '2013-05-06' => '<img class="circuit" src="liano.png" /><p class="next">Liano<br />Series<br />4-5 May 2013</p>',
        '2013-05-20' => '<img class="circuit" src="roah.png" /><p class="next">Roah<br />Series<br />18-19 May 2013</p>',
        '2013-06-03' => '<img class="circuit" src="gek.png" /><p class="next">Sano<br />Series<br />1-2 June 2013</p>',
        '2013-06-17' => '<img class="circuit" src="al.png" /><p class="next">Alaniz<br />Series<br />15-16 June 2013</p>',
        '2013-06-24' => '<img class="circuit" src="grs.png" /><p class="next">Glors<br />Series<br />22-23 June 2013</p>',
        '2013-06-08' => '<img class="circuit" src="lalall.png" /><p class="next">Lhall<br />Series<br />6-7 July 2013</p>',
        '2013-07-15' => '<img class="circuit" src="caletto.png" /><p class="next">Cetto<br />Series<br />13-14 July 2013</p>',
        '2013-07-22' => '<img class="circuit" src="orta.png" /><p class="next">Orona<br />Series<br />20-21 July 2013</p>',
        '2013-08-26' => '<img class="circuit" src="clayeon.png" /><p class="next">Claon<br />Series<br />24-25 August 2013</p>',
        '2013-09-23' => '<img class="circuit" src="pf-int.png" /><p class="next">PF<br />Series<br />21-22 September 2013</p>',
        '2013-10-07' => '<img class="circuit" src="sao.png" /><p class="next">Sano<br />Series<br />5-6 October 2013</p>',
        '2013-10-14' => '<img class="circuit" src="shenton.png" /><p class="next">Son<br />Series<br />12-13 October 2013</p>',
        '2013-11-04' => '<img class="circuit" src="casetto.png" /><p class="next">Ctto<br />Series<br />2-3 November 2013</p>',
        '2013-11-17' => '<img class="circuit" src="bahin.png" /><p class="next">Bahin<br />Series<br />15-16 November 2013</p>',
        '2014-05-01' => '<p class="nextRace">TBA</p>',
    );
    $now = new DateTime();
    $eventShown = false;
    foreach($events as $date => $event)
    {
        if ($new < new DateTime($date))
        {
            $eventShown = true;
            echo $event;
            break;
        }
    }
    if (!$eventShown)
    {
        echo "<p><strong>TBA</strong></p>";
    }
    ?>

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by flashead View Post
    Hi Rémon,

    I used your code and today it should've updated but is still just displaying the first date in the list. Have I done something wrong?

    I edited the code you sent slightly but essentially it is the same. Here it is...

    Code:
    <?php
    $events = array(
        // end date => description
        '2013-04-22' => '<img class="circuit" src="sano.png" /><p class="next">Sano<br />Series<br />20-21 April 2013</p>',
        '2013-05-06' => '<img class="circuit" src="liano.png" /><p class="next">Liano<br />Series<br />4-5 May 2013</p>',
        '2013-05-20' => '<img class="circuit" src="roah.png" /><p class="next">Roah<br />Series<br />18-19 May 2013</p>',
        '2013-06-03' => '<img class="circuit" src="gek.png" /><p class="next">Sano<br />Series<br />1-2 June 2013</p>',
        '2013-06-17' => '<img class="circuit" src="al.png" /><p class="next">Alaniz<br />Series<br />15-16 June 2013</p>',
        '2013-06-24' => '<img class="circuit" src="grs.png" /><p class="next">Glors<br />Series<br />22-23 June 2013</p>',
        '2013-06-08' => '<img class="circuit" src="lalall.png" /><p class="next">Lhall<br />Series<br />6-7 July 2013</p>',
        '2013-07-15' => '<img class="circuit" src="caletto.png" /><p class="next">Cetto<br />Series<br />13-14 July 2013</p>',
        '2013-07-22' => '<img class="circuit" src="orta.png" /><p class="next">Orona<br />Series<br />20-21 July 2013</p>',
        '2013-08-26' => '<img class="circuit" src="clayeon.png" /><p class="next">Claon<br />Series<br />24-25 August 2013</p>',
        '2013-09-23' => '<img class="circuit" src="pf-int.png" /><p class="next">PF<br />Series<br />21-22 September 2013</p>',
        '2013-10-07' => '<img class="circuit" src="sao.png" /><p class="next">Sano<br />Series<br />5-6 October 2013</p>',
        '2013-10-14' => '<img class="circuit" src="shenton.png" /><p class="next">Son<br />Series<br />12-13 October 2013</p>',
        '2013-11-04' => '<img class="circuit" src="casetto.png" /><p class="next">Ctto<br />Series<br />2-3 November 2013</p>',
        '2013-11-17' => '<img class="circuit" src="bahin.png" /><p class="next">Bahin<br />Series<br />15-16 November 2013</p>',
        '2014-05-01' => '<p class="nextRace">TBA</p>',
    );
    $now = new DateTime();
    $eventShown = false;
    foreach($events as $date => $event)
    {
        if ($new < new DateTime($date))
        {
            $eventShown = true;
            echo $event;
            break;
        }
    }
    if (!$eventShown)
    {
        echo "<p><strong>TBA</strong></p>";
    }
    ?>
    ~One of these things is not like the other....~
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    ~One of these things is not like the other....~
    Hi there!

    Thanks for the reply - how do you mean one of those things is not like the other?

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Look closer at the variable names I highlighted in red above. You've got $nOw and $nEw... The script defines $now as the current date, but $new is not initialized. turn the $new into $now, and the script works as intended.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Look closer at the variable names I highlighted in red above. You've got $nOw and $nEw... The script defines $now as the current date, but $new is not initialized. turn the $new into $now, and the script works as intended.
    You genius! Sorry, I originally looked at your correction on my phone and couldn't see the change in color in the code!

    Thank you so much - you've made my day

    Thanks again, top work!


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
  •