saintmic Posted March 20, 2007 Share Posted March 20, 2007 Hi guys. Ive searched and searched for this all over the internet and this site and the fact there seems to be no answer is a little worrying, but I'll carry on anyway. Im building a site with advert banners. What I want is a random advert from a certain file (I have different sizes) to appear at certain points on the page. To see what I mean, try http://www.321flash.es/english/template.php - there are three pictures which change when you reload. (long rectangle, middle square and square at the bottom right). The images for these are placed in seperate folders along with the following code (rotate.php) which I downloaded from another site: <?php /* -- Set $folder to the full path to the location of your images. (include rotate.php file) */ $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); } } ?> Obviously, a lot of sponsors are going to want a redirect to their website if you click on their logo. Trouble is, however I search, I cant find a system like this which will allow the images to act as individual links and is as easy as this to update (the sponsors will be changing over time and I dont want to use Javascript if i can avoid it - a) because each time we change a sponsor I'll have to rewrite the code in EVERY page and b) to cut down on loading time - my site is quite a big one to start with and I want to make it as fast as possible). Im very new to PHP so dont really understand the lingo or ways to improve it. Although, like Ive said, I searched a lot for this, I cant find anything like what Im after (and as I mentioned, Im not keen on java if I can avoid it). Can it be done, or am I wasting my time? Thanks for your help, Mic [email protected] Link to comment https://forums.phpfreaks.com/topic/43460-random-image-link-easy-update/ Share on other sites More sharing options...
monk.e.boy Posted March 20, 2007 Share Posted March 20, 2007 if i was doing this i'd set up a simple database table that holds the image name and the redirect info. Then in the PHP I'd select a random row from the database, show that image and <a href=""> it with the redirect info. You could also save the image size in the database, so you could: SELECT img,redirect FROM banners WHERE type=1 1 = tall and skinny. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/43460-random-image-link-easy-update/#findComment-211077 Share on other sites More sharing options...
saintmic Posted March 20, 2007 Author Share Posted March 20, 2007 Hi monk.e.boy, Thanks for the advice - only slight problem is that using databases confuses me even more than coding php. I understand the concept, I just cant work out how to set them up, where to store it... all number of problems. But your idea is a good one and I really appreciate the advice - thanks. Mic Link to comment https://forums.phpfreaks.com/topic/43460-random-image-link-easy-update/#findComment-211085 Share on other sites More sharing options...
monk.e.boy Posted March 20, 2007 Share Posted March 20, 2007 <?php $imgs= array(); $imgs[] = array( 'image1.png', 'http://www.site.com/index.php?tracking=99' ); $imgs[] = array( 'image2.png', 'http://www.site2.com/index.php?tracking=11' ); $banners= array(); $banners[] = array( 'banners1.png', 'http://www.site.com/index.php?tracking=99' ); $banners[] = array( 'banners2.png', 'http://www.site2.com/index.php?tracking=11' ); srand ((double) microtime( )*1000000); $random_number = rand(0,count( $imgs) ); // show img: $i = $imgs[ $random_number ]; echo '<a href="'. $i[1] .'"><img src="'. $i[0] .'"></a>'; // show banner: $i = $banners[ $random_number ]; echo '<a href="'. $i[1] .'"><img src="'. $i[0] .'"></a>'; ?> This will select a random image and banner from the two arrays. Note the site names. Note the quotes " and ' monk.e.boy Link to comment https://forums.phpfreaks.com/topic/43460-random-image-link-easy-update/#findComment-211167 Share on other sites More sharing options...
saintmic Posted March 20, 2007 Author Share Posted March 20, 2007 monk.e.boy - you are truly the stuff of legends. Thank you Link to comment https://forums.phpfreaks.com/topic/43460-random-image-link-easy-update/#findComment-211495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.