+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: Simple banner rotator

  1. #1
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,055
    Blog Entries
    9

    Simple banner rotator

    Okay today I'm going to show you how to make a simple banner rotator... You can use it for your ads on your site

    First of all we must create tables to database:

    [highlight="SQL"]
    CREATE TABLE `banner` (
    `id` int(5) NOT NULL auto_increment,
    `bannerimg` tinytext NOT NULL,
    `bannerurl` varchar(100) NOT NULL,
    `bannertxt` varchar(100) NOT NULL,
    `views` int(5) NOT NULL default '0',
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    [/highlight]

    Now we must start with our script..

    Code:
    <?php
    $SQLhost 
    "localhost";
    $SQLuser "";
    $SQLpass "";
    $DB "";
    $SQLhost = "localhost"; - Here will be your SQL host, usually it's localhost but it can be something else also.. if you want to know what's your host.. contact with your hosting provider
    $SQLuser = ""; - your database username
    $SQLpass = ""; - your database password
    $DB = ""; - Database

    Code:
    mysql_connect($SQLhost$SQLuser$SQLpass) or die("Can not connect to database: ".mysql_error());
    mysql_select_db($DB) or die("Can not select database: ".mysql_error()); 
    mysql_connect($SQLhost, $SQLuser, $SQLpass) or die("Can not connect to database: ".mysql_error()); - This will connect to the database and if it can not connect, then it will generate a error message with details why you can't connect to your database
    mysql_select_db($DB) or die("Can not select database: ".mysql_error()); - It will select a database for you.. but if it can't select it will generate a detailed error

    Now let's find out how many banners we have in our database and let's generate a random number

    Code:
    $query mysql_query("SELECT * FROM banner");
    $numTab mysql_num_rows($query);
    $RandNum = (rand()%$numTab);
    $query mysql_query("SELECT * FROM banner LIMIT $RandNum, 1"); 
    $query = mysql_query("SELECT * FROM banner"); - It will select all tables from banner table
    $numTab = mysql_num_rows($query); - This will show us how many records are in our table

    Another 2 rows will generate a random number and they will select random banner.

    Now let's show our banner.

    Code:
    while ($row mysql_fetch_array($query)){
    $banURL $row['bannerurl'];
    $banTXT $row['bannertxt'];
    $Banner $row['bannerimg'];
    $BanViews $row['views'];
    $BanID $row['id'];

    echo
    "<a href='".$banURL."' target='_blank' title='".$banTXT."'><img src='".$banner."' alt='".$banTXT."' border='0'></a>";
    $newViews $BanViews+1;
    mysql_query("UPDATE banner SET views = $newViews WHERE id = $BanID");

    while ($row = mysql_fetch_array($query)){ - Starts a while and get's info from the table
    $banURL = $row['bannerurl']; - Banner's URL
    $banTXT = $row['bannertxt']; - Banner's text
    $Banner = $row['bannerimg']; - Banner itself
    $BanViews = $row['views']; - Banner's views
    $BanID = $row['id']; - Banner's ID

    echo"<a href='".$banURL."' target='_blank' title='".$banTXT."'><img src='".$banner."' alt='".$banTXT."' border='0'></a>"; - Shows the banner that was randomly chosen from the table
    $newViews = $BanViews+1; - Increases banner views by 1
    mysql_query("UPDATE banner SET views = $newViews WHERE id = $BanID"); - Updates banner views

    And we are done Simple 'eh
    Okay here's the full script:

    Code:
    <?php

    $SQLhost 
    "localhost";
    $SQLuser "";
    $SQLpass "";
    $DB "";

    mysql_connect($SQLhost$SQLuser$SQLpass) or die("Can not connect to database: ".mysql_error());
    mysql_select_db($DB) or die("Can not select database: ".mysql_error());

    $query mysql_query("SELECT * FROM banner");
    $numTab mysql_num_rows($query);

    $RandNum = (rand()%$numTab);
    $query mysql_query("SELECT * FROM banner LIMIT $RandNum, 1");

    while (
    $row mysql_fetch_array($query)){
    $banURL $row['bannerurl'];
    $banTXT $row['bannertxt'];
    $Banner $row['bannerimg'];
    $BanViews $row['views'];
    $BanID $row['id'];

    echo
    "<a href='".$banURL."' target='_blank' title='".$banTXT."'><img src='".$banner."' alt='".$banTXT."' border='0'></a>";
    $newViews $BanViews+1;
    mysql_query("UPDATE banner SET views = $newViews WHERE id = $BanID");
    }

    ?>
    Enjoy
    Trill Hosting - Cheap SSL, Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us

    CodeCall Blog | CodeCall Wiki

  2. #2
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Simple banner rotator

    I have just started learning PHP, and I am wondering - where do you insert the SQL code? Is it in a separate file, and if so, what sort of file?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  3. #3
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25

    Re: Simple banner rotator

    Quote Originally Posted by Xav View Post
    I have just started learning PHP, and I am wondering - where do you insert the SQL code? Is it in a separate file, and if so, what sort of file?
    You insert it into your MySQL database.

  4. #4
    Programming God DevilsCharm is an unknown quantity at this point DevilsCharm's Avatar
    Join Date
    Jul 2006
    Posts
    885

    Re: Simple banner rotator

    Makes sense to insert SQL code into the MySQL database, indeed.

  5. #5
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Simple banner rotator

    I didn't know what it was at the time. I'm still a little confused, actually. Is the MySQL database a separate file, or not? And what sort of file is it?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #6
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,055
    Blog Entries
    9

    Re: Simple banner rotator


    mysql database isnt a file
    it's a database.. it's like server..
    Trill Hosting - Cheap SSL, Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us

    CodeCall Blog | CodeCall Wiki

  7. #7
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Simple banner rotator

    OOHHHH... I still don't get a couple of things. Presumably they're already made, as web hosting providers offer a set amount of databases. So why do you need to create them using sql_create_database(), if they're already there?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #8
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,055
    Blog Entries
    9

    Re: Simple banner rotator

    you can use this function also if you have permission..
    BUT
    usually you are not permitted to do it..
    so..
    you must create databases in your Cpanel or phpMyAdmin
    Trill Hosting - Cheap SSL, Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us

    CodeCall Blog | CodeCall Wiki

  9. #9
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: Simple banner rotator

    Ok. I see now.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #10
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,055
    Blog Entries
    9

    Re: Simple banner rotator

    hmm.. you gave me an idea.. i'll create some tutorials about that
    Trill Hosting - Cheap SSL, Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us

    CodeCall Blog | CodeCall Wiki

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. A simple timer
    By travy92 in forum VB Tutorials
    Replies: 3
    Last Post: 02-15-2010, 09:28 AM
  2. Project: ionFiles - Joomla Simple File Download
    By Jordan in forum Community Projects
    Replies: 369
    Last Post: 01-30-2010, 01:10 PM
  3. Tutorial - A Simple Stropwatch!
    By travy92 in forum VB Tutorials
    Replies: 19
    Last Post: 05-04-2009, 04:02 AM
  4. implementing a simple database
    By martybell in forum Visual Basic Programming
    Replies: 3
    Last Post: 11-06-2007, 04:25 PM
  5. Banner ads
    By DevilsCharm in forum Marketing
    Replies: 7
    Last Post: 07-06-2006, 11:33 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts