We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7455
    • 2,204 Posts
    I am working on my simpleslideshow snippet to make it better. now its also posible to place the snippet more then once on a page, But now I like to ad random into the snippet
    I tryed some things like randomize de count but that way it does not use all the files. so what my question is if there is a way to or read out a folder in random order or mix up the array somehow so that all the files are in the array but in random order?

    here is how far I am now:

    <?php
    
    $path = "assets/images/$folder/";
    
    if(!isset($name)) $name="slides";
    if(!isset($folder)) return; // no folder? quit.
    $interval = isset($interval) ? $interval : 5;
    $slides = array();
    if ($dh = @opendir($path)) {
      while(($file = readdir($dh)) !== false) {
         if ( !preg_match('/\.thumb*/',$file) && preg_match('@.png$|.gif$|.jpg$@', $file) && $file != '.' && $file != '..' ) {
          $slides[] = $path.$file;
        }
      }           
    }
    $num = count($slides);
    $rand= mt_rand (1 , $num-1);
    $last = $num-1;
    if(isset($js)){$script ="";}else{$script = '<script type="text/javascript" src="assets/js/slides.js"></script>';}
    $script .= '<img id="'.$name.'" src="'.$slides[$rand].' " alt="" width="'.$width.'" height="'.$height.'"/>';
    $script .= '<script type="text/javascript">';
    $script .= 'setTimeout("RunSlideShow(\"'.$name.'\",\"';
    for($i=$rand;$i<$num-1;$i++) {
      $script .= $slides[$i] . ';';
    }
    $script .= $slides[$rand] . '\",';
    $script .= $interval . ')",5000);';
    $script .= '</script>';
    unset ($rand);
    return $script;
    ?>
    


    this looks ok (well a bit) I get a random list of images somehow but never all the images that are counted, because I use $rand here: for($i=$rand;$i<$num-1;$i++) { but when I set this to 1 then its not random any more.
    I also tried to do this: $script .= $slides[$rand] . ’;’; but that way I get 20 times the same image except for the last 3 or so.

    the best way would be an option to randomize the $slides array that way it will always be random I think and you never get the same images in one slide show.

    I tried my friend Google but could not find an answer I understood, so if anyone could help me with this that would be great.
    Dimmy
      follow me on twitter: @dimmy01
      • 29181
      • 480 Posts
      If you are getting all the files into the array as you want (which is how I interpret you thread), you could use something like this (which I did to grab all the advert chunks from the database and randomise them using shuffle($the_array) )

      <?php
      //get our fields from the db and save them into a multidimensional array
      $sql = 'SELECT '.$fields.' FROM '.$tbl.' WHERE category = '.$catNo.';';
      $result = $modx->dbQuery($sql);
      $resourceArray = array();
      for($i=0;$i<@$modx->recordCount($result);$i++) {
      array_push($resourceArray,@$modx->fetchRow($result));
      }
      shuffle($resourceArray);
      $output = "";
      for ($i=0;$i<$noOfAdverts;$i++){
      $chunkname=$resourceArray[$i]['name'];
      $output .= $modx->getChunk($chunkname);
      }
      return $output;
      ?>


      Hope Im on the right tracks and that helps.
      taff
        Adrian Lawley: www.adrianlawley.com