Results 1 to 8 of 8

Thread: Add a Timer to a Random Content Changer

  1. #1
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    212
    Thanks
    151
    Thanked 1 Time in 1 Post

    Default Add a Timer to a Random Content Changer

    Hi:

    The following code can randomly choose the text to display in a web page article,

    but how can a timer be added to it? Say, I want to randomly choose to display

    different text only every 24 hours.

    Thanks

    Code:
    <?php
    
    $articles = array(
    
    'article 1 text' ,
    
    'article 2 text' ,
    
    'article 3 text',
    
    'article 4 text',
    
    'article 3 text'
    
    );
    
    $arrayNo = rand(0,4);
    
    ?>
    
     
    Then echo them out onto the page.
    
     
    <?php echo $article[$arrayNo]; ?>

  2. #2
    Join Date
    Feb 2017
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    different text only every 24 hours.
    So, at midnight (or any set time), a new day, a new text changes. To trigger that, I wouldn't know (yet). I am guessing Java.

    Chuck

  3. The Following User Says Thank You to CNT For This Useful Post:

    KennyP (02-23-2017)

  4. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,218
    Thanks
    74
    Thanked 3,384 Times in 3,345 Posts
    Blog Entries
    12

    Default

    Kenny, the question isn't clear. Do you mean you want a specific article to be shown each day? ex:

    'monday text'
    'tuesday text'
    etc.


    Or do you mean that each day you want a specific set of texts to be chosen from?

    $mondayText = Array('blah blab', 'blah blip', 'blorp');
    $tuesdayText = Array('flip', 'flop', 'bottle');
    etc.

    so on Monday it would choose from the $mondayText array, Tuesday from that day's array, and so on.

    Or maybe you want one or the other of these, but not based upon the day of the week, but based upon the date, with it rotating through however many there are and beginning again at the beginning depending upon the date number.

    What I think you're saying is have it rotate randomly, but only change every 24 hours. If that's it, you could easily have the same text two or more days in a row (random, means random, but not necessarily different), but that could be done, usually it would be different each day.

    Or perhaps you have something else in mind?
    Last edited by jscheuer1; 02-23-2017 at 05:44 AM. Reason: add option
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    KennyP (02-23-2017)

  6. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,218
    Thanks
    74
    Thanked 3,384 Times in 3,345 Posts
    Blog Entries
    12

    Default

    This might be the best solution. It will show the articles based upon the day number in the year, starting over when it gets to the end, one article per day:

    PHP Code:
    <?php

    $articles 
    = array(

    'article 1 text' ,

    'article 2 text' ,

    'article 3 text',

    'article 4 text',

    'article 5 text'

    );
    echo 
    $articles[date('z') % (count($articles) - 1)];
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    KennyP (02-23-2017)

  8. #5
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    212
    Thanks
    151
    Thanked 1 Time in 1 Post

    Default

    Thanks very much for your reply John. Sorry if my question wasn't clear. Here's exactly what I'd like to do:

    I'd like to have a folder in the root of a website called "content," filled with 1.html, 2.html, 3.html, etc. these files will contain the text to some article with some html formatting.

    Then, a div on one of the pages of that website will randomly pick a numbered file from the "content" folder every 24hrs (or whatever time is set) and display it for that one day.


    Thanks again
    Last edited by KennyP; 02-23-2017 at 08:20 AM.

  9. #6
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    408
    Thanks
    0
    Thanked 47 Times in 44 Posts

    Default

    Quote Originally Posted by KennyP View Post
    Thanks very much for your reply John. Sorry if my question wasn't clear. Here's exactly what I'd like to do:

    I'd like to have a folder in the root of a website called "content," filled with 1.html, 2.html, 3.html, etc. these files will contain the text to some article with some html formatting.

    Then, a div on one of the pages of that website will randomly pick a numbered file from the "content" folder every 24hrs (or whatever time is set) and display it for that one day.


    Thanks again
    That's exactly what John showed you how to achieve in post #4!

  10. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,218
    Thanks
    74
    Thanked 3,384 Times in 3,345 Posts
    Blog Entries
    12

    Default

    Well, if the only .html files (up to 365) in the content folder are the ones you want used for this, then it's even easier (on most servers, some might not like the include path this generates*):

    PHP Code:
    <?php
    $path 
    "../content/"//relative path to the content folder
    $articles glob("$path*.html");
    include( 
    $articles[date('z') % (count($articles) - 1)] );
    ?>
    * Also requires PHP 4.3 or later. If there's a problem with the include path, that can be changed in the code to create whatever sort of include path the server accepts. Most are fine with the one generated here.


    NOTES: This isn't exactly random. It's as random as the order of the files in the content folder* because it merely rotates day to day. But random and timing don't really go together. They can, but the coding is complex, and for anything approaching any real utility requires read/write to the file system for tracking, and still won't yield the variety to the average user that simple daily rotation will. When people say random, they usually imagine it also means unique, but that simply isn't the case. Each random selection has the same probability of being exactly the same as the previous random choice as it has of being anything different than the previous choice.

    *This can be increased slightly by using the GLOB_NOSORT flag with glob, and/or sorting the array glob creates with shuffle(), but then you get back into possibly repeating content without intending to. One can then track when to shuffle, but even then the beginning item of the new shuffle could still easily be the ending item of the previous shuffle. And one can do a routine to guarantee the previous random choice isn't repeated, but that risks occasionally creating a virtually endless loop.
    Last edited by jscheuer1; 02-24-2017 at 12:45 AM. Reason: path was confusing. later - add NOTES
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. The Following User Says Thank You to jscheuer1 For This Useful Post:

    KennyP (02-24-2017)

  12. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,218
    Thanks
    74
    Thanked 3,384 Times in 3,345 Posts
    Blog Entries
    12

    Default

    Playing around with this idea, it's become apparent that virtually any time interval is possible to use as the key for the array of pages that serve as rotating content. It could be every hour, or every 3 and a half hours, every 45 minutes, etc. what I gave you is for every 24 hours (well, strictly speaking, every day, as a day isn't always exactly 24 hours long), which I think is a good general interval, but that might depend on the number of pages and just what you're trying to achieve here. Once you start rotating content, unless it's a short slideshow, many users will have a good chance to miss certain content. Even if you run the thing on various pages, it still won't change any more frequently than specified. If you are showing it on various pages, then it might be good to have the act of navigating to a page be the trigger (or at least a trigger) to rotate it. Something at least for me to think about.

    In other developments, I've also made a rather complex function up for this that allows one to list change times in an array. The content will change each time anyone visits (activates the function by viewing a page it's running on) and the time has exceeded the next time listed in the array, showing the same content for anyone until the next time listed is exceeded and there's a visit. If no one visits during an interval change, that change will be skipped until the next visitor and the current time in effect will become the one tested for until it's exceeded, so no content will miss being shown to at least someone, but any given user may miss any given content. Files are drawn from the specified folder, and shuffled once until each has been viewed by at least one person, then they are drawn again and shuffled again (into a different order most times), and the process repeated. Requires PHP 5 or later and read/write file privileges on the server (most have this or are willing to let you turn it on). The code is well documented as to what you might want to edit. I only wish to go over it for efficiency/minor improvements and add a creditware notice (free to use as long as the credit remains in the source code) before I release it. Let me know if you're interested. It works well on my localhost server sandbox, and I've added as many error checks as I could think of to prevent - say two users running it at the same time causing issues, and other possible issues that might pop up, but there's no guarantee it's bug proof.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. random url in iframe with timer?
    By SwordOfWar in forum Looking for such a script or service
    Replies: 10
    Last Post: 11-03-2016, 01:03 PM
  2. Random Content Order script not so random?
    By robins in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 04-03-2009, 01:37 PM
  3. Random Content Order script - non-repeating random content
    By doomeyes in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 03-06-2008, 08:38 PM
  4. Random style sheet + Random content... Possible?
    By candied_ram in forum Looking for such a script or service
    Replies: 2
    Last Post: 01-12-2008, 12:23 AM
  5. Timer for a tabbed content script?
    By amason in forum JavaScript
    Replies: 0
    Last Post: 06-08-2006, 08:46 PM

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
  •