PDA

View Full Version : PHP: Rotating files - no duplicates



mobilebadboy
07-04-2004, 10:48 PM
I'm going to have a dozen or so files (containing text/images) that I'd like to randomize/rotate on the front page of a site. The thing is, I want to display roughly 3 or 4 at the same time, and I can't have duplicates.

Anyone have any insight? Say I have file1.php, file2.php, file3.php, file4.php, file5.php and file6.php. I want to display on my site:

file[x]
file[x]
file[x]

But I can't have duplicates. If I had a set number of files I could create 3 arrays with the files split between them, to assure uniques, but the number of files will always change and I'd like to have them show higher or lower at random, not have the same set of files in the same position every time.

If it can't be done with PHP, I'll accept a Javascript solution.

Best.Flash
07-05-2004, 02:05 AM
Easy enough to do in Javascript using an array randomiser:



<script language="JavaScript" type="text/JavaScript">
<!--
function fisherYates ( myArray ) {
var i = myArray.length;
while ( i-- ) {
var j = Math.floor( Math.random() * ( i + 1 ) );
var tempi = myArray[i];
var tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}
-->
</script>
<script type="text/javascript">
<!--
var asLucky = new Array ( 2, 3, 4, 5, 7, 9, 11, 13 );
fisherYates(asLucky);
var displayHowMany = 3;
for (i=0; i<displayHowMany; i++) {
var yourNumber = asLucky[i];
document.write("Random number " + i + ': ' + yourNumber + '<br>');
}
-->
</script>

mobilebadboy
07-05-2004, 12:31 PM
Thanks. It works well with numbers, but I can't seem to make it work with what I need, which is including files.

mobilebadboy
07-05-2004, 12:38 PM
Ah-ha! Now why couldn't I find this last night.

http://www.soxiam.com/archives/000034.php