4

I have a set of 4 HTML list items and I'd like to shuffle the order they appear in once a week. I was wondering if anyone out there had a nice, elegant solution to this?

As always, I'd be enormously grateful for any input you might have!

UPDATE:

Unfortunately, even with the necessary .htaccess overrides, I just can't get any srand() based solutions to work on this particular server sadly, but have the following which could be used instead - at the moment, it only returns one list item - how could I modify it to show the four required? Once again, any ideas would be gratefully received :)

function RandomList($TimeBase, $QuotesArray){

    $TimeBase = intval($TimeBase);

    $ItemCount = count($QuotesArray);

    $RandomIndexPos = ($TimeBase % $ItemCount);

    return $QuotesArray[$RandomIndexPos];

}

$WeekOfTheYear = date('W'); 

$RandomItems = array(
    "<li><a href=\"#northern-germany\" title=\"Northern Germany\">North</a></li>","<li><a href=\"#southern-germany\" title=\"Southern Germany\">South</a></li>","<li><a href=\"#western-germany\" title=\"Western Germany\">West</a></li>","<li><a href=\"#eastern-germany\" title=\"Eastern Germany\">East</a></li>");

print RandomList($WeekOfTheYear, $RandomItems);
Alex Stanhope
  • 51
  • 1
  • 6
  • 3
    I was going to post an answer that you should just use the week number as a seed value to `srand()` before calling `shuffle_array()`. This will yield the same sort order on a per-week basis. Unfortunately, it seems that this is not possible anymore since the seeding is performed automatically since PHP 4.2.0 :( – jensgram Sep 19 '11 at 09:01
  • 4
    @jensgram: that only means it is not *necessary* anymore, not that it isn't possible. I.e. your solution will work. Add it as an answer. – nikc.org Sep 19 '11 at 09:05
  • @nikc Oh, good point. I just assumed that the seed was always called but never took the time to verify. The implementation has been posted in another answer so I wont bother to write the same :) (BTW: I talked about the non-existing `shuffle_array()` function. Obviously, I meant `shuffle()`, cf cbrandolino's answer.) – jensgram Sep 19 '11 at 10:12
  • @nikc, actually it's very likely it won't work, due to the suhosin patch. See answer for details. – cbrandolino Sep 19 '11 at 10:22
  • @cbrandolino: OP never mentioned the Suhosin patch. I tested it on an installment without it and it works just dandy. – nikc.org Sep 20 '11 at 11:18
  • @nikc: the patch is now default in most distributions. What I meant is, it may be that it just isn't possible, as opposed to just not necessary. – cbrandolino Sep 20 '11 at 13:16

1 Answers1

2

Here is a simple and - I guess - pretty elegant solution, which does not involve storing values in a database, setting up cronjobs and other boring stuff of the like.

Let's pretend you have your list elements in $array:

srand(date('W'));
shuffle($array);
srand();

Now your array is shuffled, and will be shuffled the same way until next Monday.

This has a problem, though: it does not work with the Suhosin patch (installed by default in Debian). Still, now that you know about date('W') it will be easy to come up with an alternate solution yourself.

EDIT: if you don't want to implement your own pseudorandom number generator but you have Suhosin installed, you can put the following line in your .htaccess:

php_value suhosin.srand.ignore 0

cbrandolino
  • 5,873
  • 2
  • 19
  • 27
  • Thanks for the answer - looks good, but when I tried it, it seems to shuffle the order of the list items every time the page is refreshed! Any ideas what I might be doing wrong? – Alex Stanhope Sep 19 '11 at 10:31
  • You're doing nothing wrong: your php version must be shipped with the Suhosin patch. Check out the edit! – cbrandolino Sep 19 '11 at 10:39
  • 1
    Rather than using srand, you could sort by a hash seeded with the week number (some testing required to ensure even probabilities if required) or you could sort by the modulus of the index + week number for a rotating stack. – symcbean Sep 19 '11 at 11:02
  • @symcbean: He could use a disk and a magnetized needle, too. What advantage would your solution bring? – cbrandolino Sep 19 '11 at 13:56
  • @cbrandolino: no obvious advantage - just pointing out that there are multiple ways to solve the problem. – symcbean Sep 20 '11 at 08:26