lundi 30 avril 2018

Preventing array_rand string duplicate in PHP

I am randomizing php templates to include them in the web page. Each time the user reloads the web page, a new randomized template will appear.

Problem with array_rand it returns one random string each time the user reloads the web page, and the same template may be displayed again and again.

For example, let's say the user reloads the page 3 times. He may get the following templates: template1.php, template1.php, template2.php

I want the user to see all the templates in a random order. For example, template3.php,template2.php,template1.php

$items = array("template1.php","template2.php","template3.php"); #templates array
$a = $items[array_rand($items,1)]; #randomize templates

include $a; #including randomized template




Aucun commentaire:

Enregistrer un commentaire