• home
  • forum
  • my
  • kt
  • download
  • Simple banner rotator

    Author: 2007-08-11 09:29:47 From:

    To display banner ads on your website you can just sign up with Google¡¯s AdSense program or one of the many other advertising programs out there. But if you want to show your own banners or those of your website members and sponsors you should have your own banner script.

    For now we will take care of the displaying of the banners. Tracking the number of clicks will be handled in a different tutorial. So lets have a look at what we want this script to do exactly.

    If I have several banners to display I would want them all to be displayed an equal number of times. We can force this by tracking how many times each banner has been displayed and select the banner with the least number of views to show. Another way is to just select one of the banners randomly. Statistics and probability tell us that randomly selecting one of the banners each time, will over time give each banner a fairly equal amount of views. For this script we will use the random function to select which banner to display.

    To track how many times each banner has been displayed we need to add a counter to each banner. The counter will have to be updated each time we display the banner.

    In addition to the banner picture, the alt tag and title tag should be filled. Lets store all the banner information (target URL, picture URL, alt and title tag, banner views) in a MySQL database.

    Let¡¯s list the steps of our banner script:
    1. Count how many banners we have stored in our database table
    2. Create a random number between 1 and the number of banners found in step 1
    3. Retrieve the record number equal to the random number that we generated
    4. Update ¡®times viewed¡¯ counter on that record
    5. With the retrieved record data, create a link with an image and required tags.

    // database connection details
    $db_host "localhost";               
    // hostname of your MySQL server. You most likely don't have to change this
    $db_name "your_database_name";      
    // database name
    $db_user "your_database_user";      
    // database user
    $db_pass "your_database_password";  
    // database password
    $db_table"banners";                 
    // table name

    // connect to the database
    $db mysql_connect($db_host,$db_user,$db_pass
    );
    mysql_select_db ($db_name) or die ("Cannot connect to database"
    );

    // lets create a random number
    $random = (rand()%$total
    );

    // retrieve the record number corresponding to the generated random number
    $query mysql_query("SELECT * FROM ".$db_table." LIMIT $random, 1"
    );

    If the retrieval of the record was successful we can output the HTML code for the image and link. To be able to keep track of how many times each banner has been displayed, we update the counter of the record and write the new value back to the database table

    And that¡¯s all there is to creating a simple banner rotator. You can see the script in action here. The next page will show you the complete source of the script.

    <?
    // database connection details
    $db_host "localhost";               
    // hostname of your MySQL server. You most likely don't have to change this
    $db_name "your_database_name";      
    // database name
    $db_user "your_database_user";      
    // database user
    $db_pass "your_database_password";  
    // database password
    $db_table"banners";                 
    // table name

    // connect to the database
    $db mysql_connect($db_host,$db_user,$db_pass
    );
    mysql_select_db ($db_name) or die ("Cannot connect to database"
    );

    // count how many banners we have
    $query mysql_query("select * from ".$db_table.""
    );
    $total mysql_num_rows($query
    );

    // lets create a random number
    $random = (rand()%$total
    );

    // retrieve the record number corresponding to the generated random number
    $query mysql_query("SELECT * FROM ".$db_table." LIMIT $random, 1"
    );

    while (
    $row=mysql_fetch_object($query
    ))
        {
        echo
    "<a href=\"$row->ban_url\" target=\"_blank\" title=\"$row->ban_text\"><img src=\"$row->ban_image\" alt=\"$row->ban_text\" width=\"468\" height=\"60\" border=\"0\"></a>"
    ;
        
    $ban_view $row->ban_views 1
    ;
        
    // update the 'times viewed' counter on the banner
        
    mysql_query("update ".$db_table." set ban_views = $ban_view where ban_id = $row->ban_id"
    );
        }
        
    ?>

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot