#native_company# #native_desc#
#native_cta#

Random Banner Ad System

By Joe Sircy
on September 11, 2000

After reading the tip “A Small Rand Banner System” submitted by Fernando Torres, I wanted to do the same thing, but I wanted to use my database, not a text file. Some of my ads have a large chunk of code to them, i.e. JavaScript.

The first step is to create a table to store your banners. I will not go into creating tables in MySQL here, as there are numerous tutorials on php Builder for that. So create a table (I called mine “ads”) and add 2 fields:
1. autonumber, called "id"
2. text, called "data"

The second step is to create the code to retrieve the banner, selecting a random row from the “ads” table. put this code at the top of, say “test.php”.

 

<?
    # Global Varibles
    var $db, $conn;
    var $result, $rows, $data;

    $this->db = "YOUR_DB_NAME";
    $this->conn = @mysql_pconnect("HOST", "USER", "PASS") or 
        die("Unable to connect to server: '$SERVER_NAME'");

    @mysql_select_db($this->db, $this->conn) or 
        die ("Unable to select database: $this->db");


    # fuction to print ads randomly ;-) 
    function GetAds() {
      $query = "select * from ads";

      $this->result = @mysql_query($query, $this->conn) or
          die ("Unable to perform query: $query");

      $this->rows = @mysql_num_rows($this->result);

      $adNumber = rand() % $this->rows; 
      // generate a random number within the MAX number of rows

      @mysql_data_seek($this->result, $adNumber) or
         die ("Unable to seek data row: $adNumber");

      $this->data = @mysql_fetch_array($this->result) or
          die ("Unable to fetch row: $adNumber");

      echo("n<!-- ADS -->n");
      echo $this->db->data[1];  // print the "data" column
      echo("n<!-- ADS -->n");
}  

?>

Now you can call this function in the html:

 

<html>
<head><title>random banner ad test</title></head>
<body>
<center><? GetAds(); ?></center>
</body>
</html>