0

I have a set of 4 HTML list items and I'd like to shuffle the order they appear in once a week. I have found the following which could be used - but at the moment, it only returns one list item - how could I modify it to show the four required?

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);
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by cereal
0

You need a foreach loop, and the code can be more simple:

$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>");

shuffle($RandomItems);
foreach($RandomItems as $r)
{
    echo $r;
}

But you still need to save the new order for a week otherwise at next refresh the order will change, you can save it into a database or into a file, here's an example with a file, after the file is generated the script, simply includes it into the page:

<?php

$week = date('W');
$file = $week.'.txt';

$previous = date('W')-1;
$oldfile = $previous.'.txt';

$rand = array(
	array('#northern-germany','Northern Germany','North'),
	array('#southern-germany','Southern Germany','South'),
	array('#western-germany','Western Germany','West'),
	array('#eastern-germany','Eastern Germany','East')
);

function RandomList($el)
{
	shuffle($el);
	$list = '';
	foreach($el as $key => $value)
	{
	    $list .= '<li><a href="'.$value[0].'" title="'.$value[1].'">'.$value[2].'</a></li>' . "\n";
	}
	return $list;
}


if(file_exists($file))
{
	if(file_exists($oldfile))
	{
		unlink($oldfile); # delete old file
	}

	include($file); # display new order for a week

}

# create file and save new order
else
{
	$myFile = $file;
	$fh = fopen($myFile, 'w') or die("can't open file");
	$stringData = RandomList($rand);
	fwrite($fh, $stringData);
	fclose($fh);
}


?>

bye :)

Edited by cereal: n/a

0

Or, just add this to your script:

print RandomList($WeekOfTheYear, $RandomItems);
print RandomList($WeekOfTheYear-1, $RandomItems);
print RandomList($WeekOfTheYear-2, $RandomItems);
print RandomList($WeekOfTheYear-3, $RandomItems);

You will have the same result without saving files. But this should be run directly on 3 different date('W') or you could get 0 or negatives numbers on January:

print RandomList($WeekOfTheYear, $RandomItems);
print RandomList(date('W')-1, $RandomItems);
print RandomList(date('W')-2, $RandomItems);
print RandomList(date('W')-3, $RandomItems);

Bye :)

Edited by cereal: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.