By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,473 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,473 IT Pros & Developers. It's quick & easy.

Rotator - Problem with Adsense and Else

P: 73
I've been working on a small script to rotate adverts around aswell as recording how many times they've been viewed. This is working fine but what I'm trying to do now is if there is no ads to display or no active ones it will display my AdSense.

I've tried a few things which I thought should've worked but here's my code now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?
  3.    $bannerquery  = "SELECT * FROM banner WHERE active='T' ORDER BY Rand() LIMIT 1";
  4.    $bannerresult = mysql_query($bannerquery);
  5.  
  6.  
  7.    while ($brow = mysql_fetch_array($bannerresult)) {
  8.       $bannerid = $brow[0];
  9.       $bannerimage = $brow[2];
  10.       $bannerhref = $brow[1];
  11.       $banneralt = $brow[3];
  12.       $bannerhits = $brow[4];
  13.  
  14.        if($bannerid != "")
  15.    {
  16.  
  17.       echo "<a href=\"$bannerhref\" target=\"_blank\"><img src=\"images/banners/$bannerimage\" alt=\"$banneralt\" /></a>";
  18.  
  19.       /*Update Impressions*/
  20.       $newhits = $bannerhits + 1;
  21.       $bannerinsert = "UPDATE banner SET hits='$newhits' WHERE id='$bannerid'";
  22.       mysql_query($bannerinsert);
  23.    }
  24.  
  25. else
  26. {
  27. echo "<script type=\"text/javascript\"><!--
  28. google_ad_client = \"pub-*****************\";
  29. google_ad_width = 728;
  30. google_ad_height = 90;
  31. google_ad_format = \"728x90_as\";
  32. google_ad_type = \"text_image\";
  33. google_ad_channel = \"\";
  34. google_color_border = \"CCCCCC\";
  35. google_color_bg = \"CCCCCC\";
  36. google_color_link = \"333333\";
  37. google_color_text = \"000000\";
  38. google_color_url = \"333333\";
  39. //-->
  40. </script>
  41. <script type=\"text/javascript\"
  42.   src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
  43. </script>";
  44. }
  45. }
  46. ?>
  47.  
What this is doing is when there is ads to display it displays them as it should, when there's no ads or active ads to display it shows nothing instead of the adsense code.

Also, is there a simple way of implementing a system to also record how many times it has been clicked (I know it's possible, just not sure how complicated it is)?

Thanks for any help :)
Jul 20 '07 #1
Share this Question
Share on Google+
6 Replies


P: 73
Also, would somthing like what I've done be easily modified to be able to show ads that have scripts?

Thanks again.
Jul 21 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Jeigh.

Is your script outputting anything? Perhaps this line:
Expand|Select|Wrap|Line Numbers
  1. if($bannerid != "")
is not checking for the correct value. Try using empty() instead.
Jul 21 '07 #3

pbmods
Expert 5K+
P: 5,821
In terms of counting clicks, simply have the link go to an intermediate page that registers the click and then redirects the browser to the target page.
Jul 21 '07 #4

P: 73
Thank you very much for your reply, I used empty instead but it still didn't work, so I also tried moving the end bracket for the while to here:

Expand|Select|Wrap|Line Numbers
  1.    while ($brow = mysql_fetch_array($bannerresult)) {
  2.       $bannerid = $brow[0];
  3.       $bannerimage = $brow[2];
  4.       $bannerhref = $brow[1];
  5.       $banneralt = $brow[3];
  6.       $bannerhits = $brow[4];
  7.       } /*<------*/
  8.  
Instead of it being around the whole script and it worked :)

For the clicks, I'm assuming you mean have the link be <a href="clicks.php"> and within that have somthing like:

Expand|Select|Wrap|Line Numbers
  1. mysql_query("UPDATE banners SET clicks='$newclicks' WHERE id='$bannerid'");
  2.  
  3. header(Location: $url);
  4.  
But how will I get the value for $bannerid to stay the same when it goes to clicks.php as I assume it will just randomly choose another ad from the database.

Thanks.
Jul 21 '07 #5

P: 73
Never crossed my mind to put the values into the URL until now but I've got it all sorted and it's recording the clicks and redirecting to the site. I used:

Expand|Select|Wrap|Line Numbers
  1.       echo "<a href=\"redirect.php?bannerid=$bannerid&href=$bannerhref\" target=\"_blank\"><img src=\"images/banners/$bannerimage\" alt=\"$banneralt\" /></a>";
  2.  
and on redirect.php:

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. mysql_connect(localhost, *******, *******) or die(mysql_error());
  4. $dbname = '********';
  5. mysql_select_db($dbname) or die(mysql_error());
  6.  
  7. session_start();
  8.  
  9. $bannerid= trim($_GET['bannerid']);
  10.  
  11. $bannerhref= trim($_GET['href']);
  12.  
  13. $clicks = mysql_query("SELECT clicks FROM banner WHERE id='$bannerid'");
  14.  
  15. while($clickfetch = mysql_fetch_array($clicks))
  16. {
  17. $oldclicks = $clickfetch[0];
  18. }
  19.  
  20. $newclicks = $oldclicks + 1;
  21.  
  22. mysql_query("UPDATE banner SET clicks='$newclicks' WHERE id='$bannerid'");
  23.  
  24. header("Location: $bannerhref");
  25.  
  26. ?>
  27.  
  28.  
Thanks for the help :)
Jul 21 '07 #6

pbmods
Expert 5K+
P: 5,821
Heya, Jeigh.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Jul 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.