Ok, I have a random image script set up on one of my sites. the script works by droping it into the image directory and is called by linking the image location to the random.php script. I have 4 of the same script in the directory but 60% of the time the script will return the same image for atleast 2 of the images being called. How do I change the below script to always call a different image then the 1st, 2nd and 3rd script? <?php $folder = '.'; $extList = array(); $extList['gif'] = 'image/gif'; $extList['jpg'] = 'image/jpeg'; $extList['jpeg'] = 'image/jpeg'; $extList['png'] = 'image/png'; $img = null; if (substr($folder,-1) != '/') { $folder = $folder.'/'; } if (isset($_GET['img'])) { $imageInfo = pathinfo($_GET['img']); if ( isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) && file_exists( $folder.$imageInfo['basename'] ) ) { $img = $folder.$imageInfo['basename']; } } else { $fileList = array(); $handle = opendir($folder); while ( false !== ( $file = readdir($handle) ) ) { $file_info = pathinfo($file); if ( isset( $extList[ strtolower( $file_info['extension'] ) ] ) ) { $fileList[] = $file; } } closedir($handle); if (count($fileList) > 0) { $imageNumber = time() % count($fileList); $img = $folder.$fileList[$imageNumber]; } } if ($img!=null) { $imageInfo = pathinfo($img); $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ]; header ($contentType); readfile($img); } else { if ( function_exists('imagecreate') ) { header ("Content-type: image/png"); $im = @imagecreate (100, 100) or die ("Cannot initialize new GD image stream"); $background_color = imagecolorallocate ($im, 255, 255, 255); $text_color = imagecolorallocate ($im, 0,0,0); imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color); imagepng ($im); imagedestroy($im); } } ?> Code (markup): Thanks in advance!
I can't offer any help on your script but I use the following: <?php $Ad[0] = '<a href="http://www.domain.com" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxxx" border="0"></a>'; $Ad[1] = '<a href="http://www.domain.com" TARGET="_blank"><img src="images/xxxxxx.gif" alt="xxxx" border="0"></a>'; $Ad[2] = '<a href="http://www.domain.co.uk" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxxxxx" border="0"></a>'; $Ad[3] = '<a href="http://www.domain.co.uk" TARGET="_blank"><img src="images/xxxxx.gif" alt="xxxxxx" border="0"></a>'; $Ad[4] = '<a href="http://www.domain.com" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxxx" border="0"></a>'; $Ad[5] = '<a href="http://www.domaincom" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxx" border="0"></a>'; $Weight[0]=3; $Weight[1]=3; $Weight[2]=3; $Weight[3]=3; $Weight[4]=3; $Weight[5]=3; $sum =0; for($i=0;$i<count($Weight);$i++) $sum+=$Weight[$i]; $ShowAd = rand(0, $sum - 1); for($i=0;$i<count($Weight);$i++) { if($ShowAd<=$Weight[$i]) { $ShowAd=$i; break; } else $ShowAd-=$Weight[$i]; } echo $Ad[$ShowAd]; ?> PHP: You can change the weight to make images show up more or less.
To alter my script for different numbers of adverts you remove both the $Ad[5] = '<a........... and the $Weight[5]=3; lines as appropriate.
I mean I want to display more than just one advert at a time.... Like <Advert> - <Advert> - <Advert> How can I change the number of advert that will be displayed at once?
The simplest way would be to just run the script twice, you would need to have 2 different lists of adverts to prevent the same one being shown twice though.
I was looking at your original code $imageNumber = time() % count($fileList); What happens if you try $imageNumber = mt_rand (0,count($fileList));
The code I posted is more complex than it needs to be as I sometimes need to show adverts more than others. If you want to show adverts the same number of times (approx.) you can just use: $adverts=array( "htmlstring1","htmlstring2","htmlstring3","htmlstring4","htmlstring5" ); srand(time()); shuffle($adverts); for ($i=0;$i<5;++$i) // display five adverts echo "$adverts[$i]"; PHP: If you only want to show 2 adverts from the list of 5 then you just change the 5 to a 2 etc