Insert Ads Between MySQL Results
If you are pulling information from a mysql database and want to insert either ads (like adsense) or just repeat the headers after every X amount of results, here's my way of doing it:
<? //connect to the mysql database\ //I always like to define the value of $i as 0 just to keep things straight in my mind. You can set this to whatever number you want to start counting from $i=0; //The query -- this is an example from one of my sites. $result55 = mysql_query("SELECT * FROM table WHERE safe='y' AND language='english' ORDER BY timedate DESC LIMIT 10"); //Do the basic while statement while($row = mysql_fetch_array($result55)) { //Set the count of $i to plus 1 $i++; //echo your results echo $row[id]; //create an if statement and print out an ad on whatever number you like. Since I limited my query to ten results, I'm going to print an ad after the 5th result (in the middle, obviously). Note that adsense will only allow you to print three ads on any given page. if ($i == "5"){ ?> INSERT ADSENSE CODE HERE <? } ?>
See a working demo of this at www.SecretTweet.com -- the ad script is repeated after every 3rd ad when the query limits the results to 10. This way I can achieve the maximum number of ads allowed by google while spreading out the ads evenly within the results.
No Comments »
RSS feed for comments on this post. TrackBack URL