Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Vitaminwater

macrumors newbie
Original poster
Aug 18, 2009
21
0
I'm using a php script to display all rows of a mysql database. I'd like to make a google adsense ad show up at the end of each output, or after each row is displayed. So far the ad is only displaying after the first, or first two, rows are displays.

Is there another way of going about this?

THanks

Code:
  $query ="SELECT id, text";
   $query.=" FROM table ORDER BY rand() DESC";
   $result=mysql_query($query);
   while (list($id,$text) = 
    mysql_fetch_row($result)) {
echo $text;

?>

<script type="text/javascript"><!--
google_ad_client = "**";
/* 468x60, created 8/17/09 */
google_ad_slot = "*****";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<?php
}
?>
 
Try this, untested as I wrote in a hurry, but you get the idea:

PHP:
$ADSense = <<<BUFFER
<script type="text/javascript"><!--
google_ad_client = "**";
/* 468x60, created 8/17/09 */
google_ad_slot = "*****";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
BUFFER;

$query="select id, text from table order by rand() desc";
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result)) {
   
   /* Format output as you wish */
   print "ID={$row['id']} TEXT={$row['text']} <br /> $ADSense <br />";

}

Just an example that gets the database stuff one random row at a time and display the adsense code after each row, formatted as you wish. $row['id'] will contain the ID and $row['text'] will contain the text for each random row.

Quick comment - your method likely will upset visitors with all those ads displaying (understatement of the year award). Classic case of just because you can do something does not mean you should. But I supplied you some basic code to help you achieve your goal as stated in this topic.

-jim
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.