Results 1 to 4 of 4

Thread: PHP: Rotating files - no duplicates

  1. #1
    Roll Tide! mobilebadboy's Avatar
    Join Date
    Apr 2004
    Location
    Mobile, AL
    Posts
    428

    PHP: Rotating files - no duplicates

    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.
    Shawn Kerr .com

  2. #2
    Resident smart a$$
    Join Date
    Sep 2003
    Location
    UK
    Posts
    152
    Easy enough to do in Javascript using an array randomiser:

    Code:
    <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>

  3. #3
    Roll Tide! mobilebadboy's Avatar
    Join Date
    Apr 2004
    Location
    Mobile, AL
    Posts
    428
    Thanks. It works well with numbers, but I can't seem to make it work with what I need, which is including files.
    Shawn Kerr .com

  4. #4
    Roll Tide! mobilebadboy's Avatar
    Join Date
    Apr 2004
    Location
    Mobile, AL
    Posts
    428
    Ah-ha! Now why couldn't I find this last night.

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

Similar Threads

  1. Review: Professional PHP Programming
    By Chris in forum Books
    Replies: 6
    Last Post: 07-17-2013, 05:26 AM
  2. PHP Debugging help needed...
    By MarkB in forum Website Programming & Databases
    Replies: 5
    Last Post: 06-08-2004, 01:00 PM
  3. Learning how to create PHP database driven sites online...
    By incka in forum Website Programming & Databases
    Replies: 15
    Last Post: 01-23-2004, 03:18 PM
  4. PHP, ODBC, and Unicode compatibilities...
    By Stevens in forum Website Programming & Databases
    Replies: 4
    Last Post: 10-14-2003, 09:27 AM

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
  •