5

I have to show some banners in a webpage.The number of banners will be with in 10 (max 10). I can set the number of banners and each banner folder in database. Banner images are stored in separate server folders based on category. Banners are showing in columns.

My code is, Here, long1,long2,...long10 are directory names from database

 $array=array();
       for($n=1;$n<=$long;$n++)
       {
       $files = array();
       $dir=${'long'.$n};

               if(is_dir($dir))
              {
               $openDir = opendir($dir);
                       while (false !== ($file = readdir($openDir)))
                       {
                               if ($file != "." && $file != "..")
                               {
                                       $files[] = $file;
                               }
                       }
               closedir($openDir);
               }


mt_srand((double) microtime()*1000000);
 $randnum = mt_rand(0,(sizeof($files)-1));

 $arraycount=count($array);
for($index=0;$index<=$arraycount;$index++)
 {
 if(!in_array($array,$randnum))
     {
      $array[]=$randnum;
     }

 }

 $img = $dir."/".$files[$randnum];

  <input type="image" class="advt_image" src="<?=$img;?>" alt="" name=""/>
 }

ex: if there is 7 banner set in database, I have to show 7 banners from different or same folder.(some banners will be from same folder). I need to avoid the duplicate banners each time when I display a webpage.

I have assigned an array to store each random numbers. Do I need to change anything in code? any thought/idea?

Thanks!

Zendie
  • 1,176
  • 1
  • 13
  • 30
  • I'd start with an array of all possible numbers and randomly extract numbers into a new array until I had enough. That way you never repeat. – TheZ Jun 28 '12 at 07:06
  • 1
    it should not be $index<=$arraycount, it should be $index<10 (or the number of images you want to pick) – Kinjal Dixit Jun 28 '12 at 08:07

3 Answers3

1
  1. place your banner IDs in an array. Each will occur once
  2. shuffle this array using the Knuth shuffle
  3. emit the first 10 in the HTML output
Will
  • 73,905
  • 40
  • 169
  • 246
1

You can remove the image that is displayed from the $files array in the loop. this means you will have to check the length of the array in the loop as well. you could use array_diff for this.

$files = array(...); // this holds the files in the directory
$banners = array();  // this will hold the files to display
$count = 7;
for($i=0;$i<$count;$i++) {
    $c = mt_rand(0,count($files));
    $banners[] = $files[$c];
    $files = array_diff($files, array($files[$c]));
}

// now go ahead and display the $banners
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
0

A simple way to solve this problem would be create a array to hold the list of banner than before displaying them.

I didn't read your code (Sorry), but here is a basic concept, with which this is possible.

$bannerList = array();

//Now, check if the list contains the banner before adding it
while($rows) { //your big list of banners

    if(!in_array($rows['bannerpath'])) {
        $bannerList[] = $rows['bannerpath'];
    }

}
Starx
  • 77,474
  • 47
  • 185
  • 261