-4

I have 6 html files (test1.html, test2.html, test3.html and so on) and I want 3 of them to be randomly displayed with no duplicates. Then each time the web page is loaded a different order and file will be used.

Say: test1.html, test4.html, test2.html

Or: test5.html, test2.html, test3.html

Or: test3.html, test6.html, test2.html

etc.... with no duplicates

I have got this php code from a previous question which is a good start. It only includes 1 html file where I need 3?

<?php
$files = glob('*.html');
$random_file = $files[array_rand($files)];
include($random_file);
?>

Load in random .html file with PHP?

Any help is much appreciated, thanks.

Community
  • 1
  • 1
  • 3
    Sorry, but you need to learn basic programming. If you want 3 files, then you have to do something 3 times. How you go about that is up to you... 3 calls to array_rand, or a loop that iterates 3 times. – Marc B Sep 26 '13 at 16:24
  • 1
    Best 3 words of advice for today: Google "random file php" / actually that's more like four, but who's counting(?) – Funk Forty Niner Sep 26 '13 at 16:28
  • Can you accept if i answered your question please – Edward Sep 26 '13 at 16:37
  • +1 because the op has put in research effort – Edward Sep 26 '13 at 16:47

1 Answers1

2

Based on your example above.

$i = 0;
while($i < 3) {

    $randInt = rand(1, 6);
    if(isset($used)) {
        while($a < 1) {
            if(array_search($randInt, $used)) {
                $a++;
            } else {
                $randInt = rand(1, 6);
                continue;
            }
        }
    }
    include_once("path/to/test$randInt.html");
    $used[] = $randInt;
$i++;
}

Based on AD7six's idea, this is much more succinct and also allows the use of non-numerical file names:

$files = array(
    1=>'file_foo.html',
    2=>'file2_bar.html',
    3=>'file3_make.html',
    4=>'file4_it.html',
    5=>'file5_more.html',
    6=>'file6_succinct.html');

shuffle($files);

for($i=0;$i<3;$i++) {
    include_once("path/to/$files[$i]");
}
Edward
  • 1,806
  • 5
  • 26
  • 36
  • I am a novice at php when I test the file on my server "include_once('test$randInt.html');" the php code is looking for a file called "test$randInt.html" not file1.html to file6.html. Is the a glitch in the code? – Martyn Gray Sep 26 '13 at 17:15
  • see my update, i should have wrapped it in double quotes. sorry. http://php.net/manual/en/language.types.string.php – Edward Sep 26 '13 at 17:21
  • This code is a lot more than required. A loop with shuffle and array pop, for example, is simpler to read, execute and debug. – AD7six Sep 26 '13 at 17:33
  • @MartynGray add $i++; to the end of loop. AD7six don't confuse him ;) – Edward Sep 26 '13 at 17:36
  • Thanks keeping things basic it makes it easier for me to understand. I can see '$i' has 3 settings at the top of the code. But '$i++' at the bottom the loop doesn't seem to work on my server as I only get 1 test.html file. Here is a link to my php file http://www.enzygo.com/temp/index.php – Martyn Gray Sep 26 '13 at 18:50
  • I would go with the second example now its much better, the $i++; at the bottom counts at the end of the loop, it is mandatory. otherwise the loop will never finish, thats why your page won't load. – Edward Sep 26 '13 at 20:22