Rotate playlist every 24 hours

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Rotate playlist every 24 hours

Post by paraleadogg »

Hi guys,

I wonder if anyone could help?

I have seen lots of scripts etc to rotate images, links etc but nothing that really does what I am looking for....

Basically I have a playlist (a list of urls to flash videos).

There are 20 urls in this playlist.

I would like to simply rotate the playlist every 24 hours.

So the url in position 1 moves to position 2....and the url in position 20 moves to position 1....etc

For example (this example uses just 5 urls...)

day 1...............day 2...............day 3.............etc

url1..................url5.................url4
url2..................url1.................url5
url3..................url2.................url1
url4..................url3.................url2
url5..................url4.................url3

I am sure this is simple enough - is there an easy function I can use to sort an array or a list like this?

The list should change every 24 hours

Thank you very much in advance for any advice and I hope I have explained the problem clealy enough

Cheers
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Rotate playlist every 24 hours

Post by requinix »

Method that doesn't involve cron:

Make the playlist a PHP file that includes the right header(Content-Type:...) so nobody gets confused. Then, depending on the current time, print the playlist URLs in the right order.

20 URLs cycled once a day means it takes 20 days to go through all of them. If you can figure out which day in that 20-day cycle you're currently in then you know which URL should be printed first (and second, and third...). Just takes a little bit of math.

It's complicated so I'll get you started. Pay attention to the /*...*/ comments: you (as the person asking for help on this forum) should read them.

Code: Select all

<?php
 
/* stuff that should change */
$urls = array("url 1", "url 2", "url 3", "...", "url 19", "url 20");
$cycle = 86400; // number of seconds to cycle the URLs
 
$count = count($urls); // 6 here, would be 20
$repeat = $count * $cycle; // how many seconds it takes to cycle back to the beginning
 
$time = time();
/* or, if you want to begin at a certain date/time, */
//$time = time() - mktime(hour, minute, second, month, day, year);
 
$period = floor(($time % $repeat) / $cycle); // which period we're in now
 
// ready to start the playlist
// first, send the right header so nobody thinks this is supposed to be HTML
header("Content-Type: ???");
 
// now start printing the playlist
for ($i = 0; $i < $count; $i++) {
    /* if first becomes last (1,2,3,4 -> 2,3,4,1) */
    //$index = ($i + $period) % $count;
    /* if last becomes first (1,2,3,4 -> 4,1,2,3) */
    $index = ($count - $period + $i) % $count;
 
    $url = $urls[$index];
 
    echo "URL: ", $url, "\n";
}
 
?>
Oh yeah. If you tell me what kind of playlist format you're using then I'll tell you what to replace those ???s with (line 16). I can also help you with how to print the playlist.

Incidentally, if you run that today you won't see any reordering. If you look at lines 9 and 10 you'll see that you can set a starting date: play around with that and you'll see it works.
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Re: Rotate playlist every 24 hours

Post by paraleadogg »

Hey cool - thanks for your help

At the minute the "playlist" is just a hardcoded list of urls (pointing at videos on blip.tv) within my php page

These are then loaded into flowplayer for streaming

Eventually though the urls will be loaded via an xml file or maybe even via a database

So - the videos will reside in a folder on my server, and url, title etc will be loaded in via an xml file

Once the urls are loaded from my XML file I need to be able to rotate them each day

Does this make any kind of sense??

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Rotate playlist every 24 hours

Post by requinix »

Yeah, makes sense. The XML will, at some point in its structure, resemble an array. Using SimpleXML as an example,

Code: Select all

<?php
 
$xml = simplexml_load_string(<<<XML
<?xml version="1.0" encoding="utf-8"?>
<playlist>
    <video>
        <title>First</title>
        <url>http://example.com/first.mpg</url>
    </video>
    <video>
        <title>Second</title>
        <url>http://example.com/second.mpg</url>
    </video>
</playlist>
XML
);
 
/* stuff that should change */
//$urls = array("url 1", "url 2", "url 3", "...", "url 19", "url 20"); no need for this
$cycle = 86400; // number of seconds to cycle the URLs
 
$count = count($xml->video); // 2
 
/* ... */
 
for ($i = 0; $i < $count; $i++) {
    /* if first becomes last (1,2,3,4 -> 2,3,4,1) */
    //$index = ($i + $period) % $count;
    /* if last becomes first (1,2,3,4 -> 4,1,2,3) */
    $index = ($count - $period + $i) % $count;
 
    $url = (string)$xml->video[$index]->url; /* only thing that changed in here */
 
    echo "URL: ", $url, "\n";
}
Using $index in the loop you make $url be whatever it's supposed to be.

(If you do it from a database it's a bit more complicated but a fairly simple task.)

Any problems?
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Re: Rotate playlist every 24 hours

Post by paraleadogg »

Hey - Yes thats great....getting close.....couple of issues though

You declare $cycle = 86400; (for the 24 hours)

But then this "$cycle" variable is not used?? Or am i missing something?

And also I need to get all the urls.....not just echo out the top one

Basically within the page the list of urls you should resembles this

Day 1

<a href="video/video1.flv">video 1</a>
<a href="video/video2.flv">video 2</a>
<a href="video/video3.flv">video 3</a>
<a href="video/video4.flv">video 4</a>
<a href="video/video5.flv">video 5</a>

Day 2

<a href="video/video5.flv">video 5</a>
<a href="video/video1.flv">video 1</a>
<a href="video/video2.flv">video 2</a>
<a href="video/video3.flv">video 3</a>
<a href="video/video4.flv">video 4</a>

Day 3

<a href="video/video4.flv">video 4</a>
<a href="video/video5.flv">video 5</a>
<a href="video/video1.flv">video 1</a>
<a href="video/video2.flv">video 2</a>
<a href="video/video3.flv">video 3</a>

etc etc

Thank you for your time and help

I do appreciate it : )
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Rotate playlist every 24 hours

Post by requinix »

paraleadogg wrote:You declare $cycle = 86400; (for the 24 hours)

But then this "$cycle" variable is not used?? Or am i missing something?
It's used in a couple computations but that's it.
paraleadogg wrote:And also I need to get all the urls.....not just echo out the top one
Right. It does that. Try running the first bit of code I posted.
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Re: Rotate playlist every 24 hours

Post by paraleadogg »

Aaaaaaah ok. Sorry.

I'm tired and forgot about the first snippet of code you posted :oops:

That looks great

Thank you very much :D
Post Reply