Rotator - Problem with Adsense and Else

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeigh
    New Member
    • Jul 2007
    • 73

    Rotator - Problem with Adsense and Else

    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:

    [code=php]

    <?
    $bannerquery = "SELECT * FROM banner WHERE active='T' ORDER BY Rand() LIMIT 1";
    $bannerresult = mysql_query($ba nnerquery);


    while ($brow = mysql_fetch_arr ay($bannerresul t)) {
    $bannerid = $brow[0];
    $bannerimage = $brow[2];
    $bannerhref = $brow[1];
    $banneralt = $brow[3];
    $bannerhits = $brow[4];

    if($bannerid != "")
    {

    echo "<a href=\"$bannerh ref\" target=\"_blank \"><img src=\"images/banners/$bannerimage\" alt=\"$banneral t\" /></a>";

    /*Update Impressions*/
    $newhits = $bannerhits + 1;
    $bannerinsert = "UPDATE banner SET hits='$newhits' WHERE id='$bannerid'" ;
    mysql_query($ba nnerinsert);
    }

    else
    {
    echo "<script type=\"text/javascript\"><!--
    google_ad_clien t = \"pub-*************** **\";
    google_ad_width = 728;
    google_ad_heigh t = 90;
    google_ad_forma t = \"728x90_as\ ";
    google_ad_type = \"text_image \";
    google_ad_chann el = \"\";
    google_color_bo rder = \"CCCCCC\";
    google_color_bg = \"CCCCCC\";
    google_color_li nk = \"333333\";
    google_color_te xt = \"000000\";
    google_color_ur l = \"333333\";
    //-->
    </script>
    <script type=\"text/javascript\"
    src=\"http://pagead2.googles yndication.com/pagead/show_ads.js\">
    </script>";
    }
    }
    ?>
    [/code]

    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 :)
  • Jeigh
    New Member
    • Jul 2007
    • 73

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

    Thanks again.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Jeigh.

      Is your script outputting anything? Perhaps this line:[code=php]if($bannerid != "")[/code] is not checking for the correct value. Try using empty() instead.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        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.

        Comment

        • Jeigh
          New Member
          • Jul 2007
          • 73

          #5
          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:

          [code=php]
          while ($brow = mysql_fetch_arr ay($bannerresul t)) {
          $bannerid = $brow[0];
          $bannerimage = $brow[2];
          $bannerhref = $brow[1];
          $banneralt = $brow[3];
          $bannerhits = $brow[4];
          } /*<------*/
          [/code]

          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.ph p"> and within that have somthing like:

          [code=php]
          mysql_query("UP DATE banners SET clicks='$newcli cks' WHERE id='$bannerid'" );

          header(Location : $url);
          [/code]

          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.

          Comment

          • Jeigh
            New Member
            • Jul 2007
            • 73

            #6
            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:

            [code=php]
            echo "<a href=\"redirect .php?bannerid=$ bannerid&href=$ bannerhref\" target=\"_blank \"><img src=\"images/banners/$bannerimage\" alt=\"$banneral t\" /></a>";
            [/code]

            and on redirect.php:

            [code=php]
            <?

            mysql_connect(l ocalhost, *******, *******) or die(mysql_error ());
            $dbname = '********';
            mysql_select_db ($dbname) or die(mysql_error ());

            session_start() ;

            $bannerid= trim($_GET['bannerid']);

            $bannerhref= trim($_GET['href']);

            $clicks = mysql_query("SE LECT clicks FROM banner WHERE id='$bannerid'" );

            while($clickfet ch = mysql_fetch_arr ay($clicks))
            {
            $oldclicks = $clickfetch[0];
            }

            $newclicks = $oldclicks + 1;

            mysql_query("UP DATE banner SET clicks='$newcli cks' WHERE id='$bannerid'" );

            header("Locatio n: $bannerhref");

            ?>

            [/code]

            Thanks for the help :)

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Jeigh.

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

              Comment

              Working...