PHP .rand函数,

My php knowledge is limited so sorry if answer is obvious.

I'm using the .rand function to display random images from one of 3 pages when a link is clicked. The pages are all named the same and are in different folders (pages,media,text). The problem I'm having is that I want a combination of 3 different pages/images every time but instead it comes out as the same 3 page/image combination each time. I want this e.g. Page1, Media2, Text1. next... Page3, Media2, Text3. next... Page1, Media3, Text2.

Instead it comes out e.g. Page1, Media1, Text1. next... Page3, Media3, Text3. next... Page2, Media2, Text2.

The solution is probably simple but I can't seem to figure it out.

here is my code :

<body>
<div id="wrapper">
  <div id="click"><a href="index.php?p=<?php echo 'include-'.rand(1,3); ?>">CLICK HERE</a>></div>
  <div id="content">
    <?php 
    if (!empty($_GET['p'])) {
        $pages_dir = 'pages';
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);

        $p = $_GET['p'];

        if (in_array($p.'.inc.php',$pages)) {
            include($pages_dir.'/'.$p.'.inc.php');
        }   else {
            echo 'Sorry, page not found.';
        }
  } 
  ?>
  </div>
  <div id="content2">
    <?php
    if (!empty($_GET['p'])) {
        $media_dir = 'media';
        $media = scandir($media_dir, 0);
        unset($media[0], $media[1]);

        $p = $_GET['p'];

        if (in_array($p.'.inc.php',$media)) {
            include($media_dir.'/'.$p.'.inc.php');
        }   else {
            echo 'Sorry, page not found.';
        }
  } 
  ?>
  </div>
   <div id="content3">
    <?php
    if (!empty($_GET['p'])) {
        $text_dir = 'text';
        $text = scandir($text_dir, 0);
        unset($text[0], $text[1]);

        $p = $_GET['p'];

        if (in_array($p.'.inc.php',$text)) {
            include($text_dir.'/'.$p.'.inc.php');
        }   else {
            echo 'Sorry, page not found.';
        }
  } 
  ?>
  </div>
</div>
</body>

you have used the function "rand()" just once , so you will have just one random number. If you want more numbers , use the function multiple times. for example :

$rnd_1 = rand(0,3);
$rnd_2 = rand(0,3);
$rnd_3 = rand(0,3);

Each of them may produce a unique number ;)

You use rand only once here:

<a href="index.php?p=<?php echo 'include-'.rand(1,3); ?>">

There are two solutions:

  • Don't use rand as GET parameter, just generate random numbers when $_GET['p'] is set.

  • Create another two GET parameters, ex. $_GET['m'] for media, $_GET['c'] for content and replace $_GET['p'] with them.

Try using shuffle(glob('*.png'));, or just shuffle(); an array of your pictures. See:

http://php.net/manual/en/function.glob.php

and

http://php.net/manual/en/function.shuffle.php

The string inside glob can be replaced with any file extension.

Your code might look like this:

$allPics = shuffle(glob('*png')); $pic = array_slice($allPics, 0, 3);

Now $pic[0], $pic[1], and $pic[2] hold your images.

Personally, I Think You Should Use JavaScript if you're just trying to return 3 images. That code would look something like this this:

First the External picloader.js file:

//+ Jonas Raoni Soares Silva - Google
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
  for(var j,x,i=o.length; i; j=parseInt(Math.random()*i),x=o[--i],o[i]=o[j],o[j]=x);
    return o;
}
//everything else by Jason Raymond Buckley
function E(e){
  return document.getElementById(e);
}
function picLoader(clickId, outIdsArray, imageArray){
  E(clickId).onclick = function(){
    var oa = outIdsArray, il = oa.length, sh = shuffle(imageArray), pic = sh.slice(0, il);
    for(var i in oa){
      E(oa[i]).innerHTML = "<img src='"+pic[i]+"' />";
    }
  }
}

The code on your page should now look like:

<body>
  <div id="wrapper">
    <input type='button' value='CLICK HERE' id='showPics' />
    <div id="content"></div>
    <div id="content2"></div>
    <div id="content3"></div>
  </div>
  <script type='text/javascript' src='picloader.js'></script>
  <script type='text/javascript'>
    var imageArray = ['img1.png', 'img2.png', 'img3.png', 'img4.png', 'img5.png', 'img6.jpg', 'img7.jpg'];
    picLoader('showPics', ['content', 'content1', 'content2'], imageArray);
  </script>
</body>
</html>

This should work for any amount of output elements, as long as the imageArray is at least as long as your array that contains output content ids.