MishelM Posted March 17, 2010 Share Posted March 17, 2010 Ok I am trying to rotate ads on a page ( no big deal.. very simple ad rotation script) HOWEVER how do I ensure that the same ads do not show on the same page? What would I need to ad to the following code? I am using this code between <head> and </head> <script type="text/javascript"> function reload() { window.location.reload(); } function countdown() { setTimeout("reload()", 10000); } </script> Then I am using this in the individual ad placement slots <?php include("config.php"); $random = rand(0, count($image)-1); echo "<p><a href=\"" . $link[$random] . "\"><img src=\"" . $image[$random] . "\" border="0" /></a></p>"; ?> Then I have aconfig.php file which looks like this <?php $image[0] = "Ads/Ad_01.jpg"; $link[0] = "http://www.mysite.org/myproduct"; $image[1] = "Ads/Ad_02.jpg"; $link[1] = "http://www.mysite.org/myproduct"; ?> ( there are more links but you get the drift) Is there anyone that can help me- I am a beginner so be gentle!!!! Quote Link to comment Share on other sites More sharing options...
MishelM Posted March 17, 2010 Author Share Posted March 17, 2010 Puleeeeeease... there has to be someone who can help me- I need someone who knows more than I do ( which is probably everyone on this forum)... can someone take a minute and help? Quote Link to comment Share on other sites More sharing options...
fr34k Posted March 17, 2010 Share Posted March 17, 2010 Try moving this to the top of your PHP page: <?php require_once("config.php"); ?> And put this in your advertisement areas: <?php $random = rand(0, count($image)-1); echo "<p><a href=\"" . $link[$random] . "\"><img src=\"" . $image[$random] . "\" border="0" /></a></p>"; unset($link[$random], $image[$random]); ?> I think that'll do it for you. It's not an optimal solution, but it's the easiest. By the way, there's tons of room for improvement here. You should look into associative arrays and the array_rand function. Quote Link to comment Share on other sites More sharing options...
MishelM Posted March 17, 2010 Author Share Posted March 17, 2010 Thank you soooooooo much- I will try this!!! Quote Link to comment Share on other sites More sharing options...
MishelM Posted March 17, 2010 Author Share Posted March 17, 2010 Ok I keep getting a syntax error with this... any ideas?? Forgive me when I say I am a newbie ... I am REALLY a newbie Quote Link to comment Share on other sites More sharing options...
fr34k Posted March 17, 2010 Share Posted March 17, 2010 Ok I keep getting a syntax error with this... any ideas?? Forgive me when I say I am a newbie ... I am REALLY a newbie Can you post the exact error and code associated with the line(s)? Quote Link to comment Share on other sites More sharing options...
MishelM Posted April 15, 2010 Author Share Posted April 15, 2010 Ok this code is actually working now however, it sometimes repeats the same ad twice on a page- what can I add to this code to prevent that? Could someone please have pity on me? Puleeeeease? Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 15, 2010 Share Posted April 15, 2010 just a thought... put all possible ads into an array; shuffle the array; as you go thru the array displaying ads remove the ad from the array Quote Link to comment Share on other sites More sharing options...
MishelM Posted April 15, 2010 Author Share Posted April 15, 2010 I am not quite sure how to do that Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 15, 2010 Share Posted April 15, 2010 A rough idea... /* first put all your ads into an array */ /* for example */ $my_ads = array("image1.jpg", "image9.jpg", "imageabc.jpg", "image_me.jpg", "imagew.jpg", "imagede.jpg", "image01.jpg", ); /* shuffle the array */ $my_ads = shuffle($my_ads); /* display the first element and remove it from the array */ /* repeat this step as often as necessary */ /* NOTE: make sure you only do it as often as there are still ads in the array */ $what_ad = array_shift($my_ads); echo $what_ad; Quote Link to comment Share on other sites More sharing options...
mctoys Posted April 15, 2010 Share Posted April 15, 2010 How about in the individual Ad slots using $random+1 or $random+2 etc to ensure no duplication? Quote Link to comment Share on other sites More sharing options...
MishelM Posted April 16, 2010 Author Share Posted April 16, 2010 Ok here is what I am using in the individual ad slots right now- ( see below) would I place $random+1 in the place I have highlighted in bold? If I had four slots would I use +1, +2 etc ? Sorry for being so thick but this is all pretty new to me. I really appreciate the help!! <?php $random = rand(0, count($image)-1); echo "<p><a href=\"" . $link[$random] . "\"><img src=\"" . $image[$random] . "\" alt=\"My affiliate\" /></a></p>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.