php - How to avoid to display duplicates from random images script? -


the following script perfect needs unfortunately displays duplicate images.

how can modify fix issue?

or in place of there similar script allowing display random images in 1 column , each of them own link?

thanks.

<?php  function display_random_img($array) { $key = rand(0 , count($array) -1); $link_url = $array[$key]['url']; $alt_tag = $array[$key]['alt']; $random_img_url = $array[$key]['img_url']; list($img_width, $img_height) = getimagesize($random_img_url); return "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>"; } //----------------------- $ads_array = array( array(     'url' => 'http://www.mysite.com/',     'alt' => 'image1',     'img_url' => 'http://www.mysite.com/pic1.jpg' ), array(     'url' => 'http://www.yoursite.com/',     'alt' => 'image2',     'img_url' => 'http://www.mysite.com/pic2.jpg' ), array(     'url' => 'http://www.theirsite.com/',     'alt' => 'image3',     'img_url' => 'http://www.mysite.com/pic3.jpg' ) ); //----------------------- $ads_array_1 = array(  array(     'url' => 'http://www.mysite.com/',     'alt' => 'image1',     'img_url' => 'http://www.mysite.com/pic1.jpg' ), array(     'url' => 'http://www.yoursite.com/',     'alt' => 'image2',     'img_url' => 'http://www.mysite.com/pic2.jpg' ), array(     'url' => 'http://www.theirsite.com/',     'alt' => 'image3',     'img_url' => 'http://www.mysite.com/pic3.jpg' ) );  //----------------------- $ads_array_2 = array(  array(     'url' => 'http://www.mysite.com/',     'alt' => 'image1',     'img_url' => 'http://www.mysite.com/pic1.jpg' ), array(     'url' => 'http://www.yoursite.com/',     'alt' => 'image2',     'img_url' => 'http://www.mysite.com/pic2.jpg' ), array(     'url' => 'http://www.theirsite.com/',     'alt' => 'image3',     'img_url' => 'http://www.mysite.com/pic3.jpg' ) );  //----------------------- echo display_random_img($ads_array); echo display_random_img($ads_array_1);  echo display_random_img($ads_array_2); ?> 

if have 3 images in set of images chance of 1/3 image displayed 2 times. increase list of images.

however, should use mt_rand() give better randomness. main problem small amount of images


update:

after thinking little bit question, think need this:

you'll need 1 array:

$ads_array = array( array(     'url' => 'http://www.mysite.com/',     'alt' => 'image1',     'img_url' => 'http://www.mysite.com/pic1.jpg' ), array(     'url' => 'http://www.yoursite.com/',     'alt' => 'image2',     'img_url' => 'http://www.mysite.com/pic2.jpg' ), array(     'url' => 'http://www.theirsite.com/',     'alt' => 'image3',     'img_url' => 'http://www.mysite.com/pic3.jpg' ) ); 

and function uses shuffle() generate randomness:

function display_random_images($array, $maxcount = 3) {     // shuffle $array elements     shuffle($array);     $html = '';     for($i = 0; $i < min(count($array), $maxcount); $i++) {         $img = $array[$i];         $link_url = $img['url'];         $alt_tag = $img['alt'];         $random_img_url = $img['img_url'];         list($img_width, $img_height) = getimagesize($random_img_url);         $html .= "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";     }     return $html; } 

now call function, output $maxcount images, per default 3

echo display_random_images($ads_array); 

if have more images, can call this:

echo display_random_images($ads_array, 10); // 10 imgs or whatever 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -