php - pic random images from array -


so have code randomly pics image out of array provided:

$bg = array('1.jpg', '2.jpg', '2.jpg',); // array of filenames  $i = rand(0, count($bg)-1); // generate random number size of array $selectedbg = "$bg[$i]"; // set variable equal random filename chosen 

but images folder no matter name , how many there are.

i've tried :

$dir = "bg_photos/less_saturation/";  $exclude = array( ".",".."); if (is_dir($dir)) {     $files = scandir($dir);     foreach($files $file){         if(!in_array($file,$exclude)){             echo $file;                      $bg = array($file); // array of filenames             $i = rand(0, count($bg)-1); // generate random number size of array             $selectedbg = "$bg[$i]"; // set variable equal random filename chosen         }     } } 

but ever gives me last image in array ... can ?

cheers

chris

you can use code. collect files, exept . , .., array, , random item array.

$dir = "bg_photos/less_saturation/"; $exclude = array( ".",".."); $bg = array(); if (is_dir($dir)) {     $files = scandir($dir);     foreach($files $file){         if(!in_array($file,$exclude)){             echo $file;                      //use array             $bg[] = $file; // array of filenames         }     } } $selectedbg = $bg[array_rand($bg)]; 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -