JavaScript
  Home arrow JavaScript arrow Page 3 - Storing Banner Data in MySQL Tables for a ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Storing Banner Data in MySQL Tables for a Dynamic Banner System with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-08-01

    Table of Contents:
  • Storing Banner Data in MySQL Tables for a Dynamic Banner System with AJAX
  • Listing the entire source files of the previous banner application
  • Fetching banner data from MySQL
  • Completing the banner application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Storing Banner Data in MySQL Tables for a Dynamic Banner System with AJAX - Fetching banner data from MySQL


    (Page 3 of 4 )

    In accordance with the concepts expressed in the previous section, the next step is to create a primitive database table for storing the images and URLs of every banner that needs to be displayed.

    Based upon this schema, below I included the definition of a sample "banners" MySQL table, which has been populated with the banner data used in the two previous articles of the series.

    This MySQL database table looks like this:

    id                                 image                           url

    1                                  banner1.gif                    http://www.myhosting.com
    2                                  banner2.gif                    http://www.myphp.com
    3                                  banner3.gif                    http://www.myjs.com

    The above table was indeed easy to create, right? As you can see, all of the banner-related data now resides neatly in a basic database, which implies that pulling out the pertinent records is reduced to using the same pair of JavaScript functions that you learned previously.

    Therefore, now the complete client-side code required to fetch the banners from the MySQL database table defined earlier would look like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />

    <title>AJAX-Driven Dynamic Banner System</title>

    <script language="javascript" type="text/javascript">

    // send http requests

    function sendHttpRequest(url,callbackFunc,respXml){

                var xmlobj=null;

                try{

                            xmlobj=new XMLHttpRequest();

                }

                catch(e){

                            try{

                                                   xmlobj=new
    ActiveXObject("Microsoft.XMLHTTP");

                            }

                catch(e){

                                       alert('AJAX is not supported
    by your browser!');

                                       return false;

                }

       }

       xmlobj.onreadystatechange=function(){

                if(xmlobj.readyState==4){

                                       if(xmlobj.status==200){

                                       respXml?eval
    (callbackFunc+'(xmlobj.responseXML)'):eval
    (callbackFunc+'(xmlobj.responseText)');

                                       }

                }

        }

        // open socket connection

        xmlobj.open('GET',url,true);

        // send http header

        xmlobj.setRequestHeader('Content-Type','text/plain;
    charset=UTF-8');

        // send http request

        xmlobj.send(null);

    }

    // display banners

    function displayBanner(bannerData){

                // parse banner data

                var bannerImg=bannerData.split('|')[0];

                if(!bannerImg){return};

                var bannerUrl=bannerData.split('|')[1];

                if(!bannerUrl){return};

                var bannerCont=document.getElementById
    ('bannercontainer');

                if(!bannerCont){return};

                // clean up banner container

                bannerCont.innerHTML='';

                // create banner link

                var a=document.createElement('a');

                a.setAttribute('href',bannerUrl);

                // create banner image

                var img=document.createElement('img');

                // set banner image dimensions

                img.setAttribute('src',bannerImg);

                img.setAttribute('width',180);

                img.setAttribute('height',400);

                // append banner image to link

                a.appendChild(img);

                // append banner link to container

                bannerCont.appendChild(a);

                // fetch banner recursively

                setTimeout("sendHttpRequest
    ('fetchbanner.php','displayBanner')",15*1000);

    }

    window.onload=function(){

                if(document.getElementById &&
    document.getElementsByTagName && document.createElement){

                            // fetch first banner

                            sendHttpRequest
    ('fetchbanner.php','displayBanner');

                }

    }

    </script>

    <style type="text/css">

    body{

                margin: 0;

                padding: 0;

                background: #eee;

    }

    h1{

                text-align: center;

                font: bold 24px Arial, Helvetica, sans-serif;

                color: #000;

    }

    #bannercontainer{

                text-align: center;

                width: 180px;

                height: 400px;

                margin-left: auto;

                margin-right: auto;

                background: #fff;

                border: 1px solid #000;

    }

    #bannercontainer img{

                border: none;

    }

    </style>

    </head>

    <body>

    <h1>AJAX-Driven Dynamic Banner System</h1>

    <div id="bannercontainer"></div>

    </body>

    </html>

    As shown above, the general structure of this banner system remains nearly identical, since it uses the same JavaScript functions defined in the previous articles of the series. Of course, in this case the different banners will be fetched at a predefined time interval from the prior "banners" database table, instead of using a XML file, but the rest of the application's logic remains unmodified.

    Okay, now that you have seen how easy it was to adapt the original structure of this banner application to fetch the banners from a basic MySQL database table, it's time to develop a simple PHP script. This script will be tasked with sending the appropriate banner data to the browser each time an AJAX-based HTTP request is triggered by the application.

    To learn how this script will be built, please go ahead and read the next few lines. I'll be there, waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the course of this last installment of the series, the original banner...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT