Php small code - Need a little help in correcting him

Viewed 47

I need your help to advise me in correcting the php code to work properly. Thank you in advance!

If i had 10 products, the random function is no longer working, shows only 10 static results (no random). The random function is working perfectly only if i put 9 products. My request is to show all products (10) in random mode.

My PHP Code

 $files = array(
 "products/page1.php",
 "products/page2.php",
 "products/page3.php",
 "products/page4.php",
 "products/page5.php", 
 "products/page6.php", 
 "products/page7.php",
 "products/page8.php", 
 "products/page9.php", 
 "products/page10.php"
 );
 foreach (array_rand($files, 10) as $file) {
 include($files[$file]);
 }
?>

1 Answers

Version 1 - My initial code

$files = array(
 "products/page1.php",
 "products/page2.php",
 "products/page3.php",
 "products/page4.php",
 "products/page5.php", 
 "products/page6.php", 
 "products/page7.php",
 "products/page8.php", 
 "products/page9.php", 
 "products/page10.php"
 );
 shuffle($files);
 foreach (array_rand($files, 10) as $file) {
 include($files[$file]);
 }
?>

Version 2 | Thanks to Marcin Orlowski

<?php
 $files = [
 "products/page1.php",
 "products/page2.php",
 "products/page3.php",
 "products/page4.php",
 "products/page5.php", 
 "products/page6.php", 
 "products/page7.php",
 "products/page8.php", 
 "products/page9.php", 
 "products/page10.php"
 ];
 shuffle($files);
 foreach (array_rand($files, 10) as $file) {
 require($files[$file]);
 }
?>

Version 3 | Thanks to brombeer

<?php
 $files = [
 "products/page1.php",
 "products/page2.php",
 "products/page3.php",
 "products/page4.php",
 "products/page5.php", 
 "products/page6.php", 
 "products/page7.php",
 "products/page8.php", 
 "products/page9.php", 
 "products/page10.php"
 ];
 shuffle($files);
foreach ($files as $key => $file) { require($file); }
?>
Related