Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials
Register Blogs Search Today's Posts Mark Forums Read

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-2008, 06:59 AM
Jaan's Avatar
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,987
Jaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to behold
Send a message via MSN to Jaan
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:

SQL Code:
  1. CREATE TABLE `banner` (
  2.   `id` int(5) NOT NULL AUTO_INCREMENT,
  3.   `bannerimg` tinytext NOT NULL,
  4.   `bannerurl` varchar(100) NOT NULL,
  5.   `bannertxt` varchar(100) NOT NULL,
  6.   `views` int(5) NOT NULL DEFAULT '0',
  7.   PRIMARY KEY  (`id`)
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Now we must start with our script..

PHP 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

PHP 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

PHP 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.

PHP 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:

PHP 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
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-02-2008, 02:40 PM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-02-2008, 03:20 PM
John's Avatar
Co-Administrator
 
Join Date: Jul 2006
Age: 21
Posts: 5,835
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
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.
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-18-2008, 08:34 PM
DevilsCharm's Avatar
Programming God
 
Join Date: Jul 2006
Posts: 885
DevilsCharm is an unknown quantity at this point
Re: Simple banner rotator

Makes sense to insert SQL code into the MySQL database, indeed.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-20-2008, 03:33 PM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-21-2008, 08:35 AM
Jaan's Avatar
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,987
Jaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to behold
Send a message via MSN to Jaan
Re: Simple banner rotator


mysql database isnt a file
it's a database.. it's like server..
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-21-2008, 03:09 PM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-22-2008, 07:13 AM
Jaan's Avatar
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,987
Jaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to behold
Send a message via MSN to Jaan
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
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-22-2008, 11:36 AM
Xav's Avatar
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-22-2008, 02:21 PM
Jaan's Avatar
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,987
Jaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to beholdJaan is a splendid one to behold
Send a message via MSN to Jaan
Re: Simple banner rotator

hmm.. you gave me an idea.. i'll create some tutorials about that
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 369 01-30-2010 02:10 PM
A simple timer travy92 VB Tutorials 2 01-22-2010 10:29 AM
Tutorial - A Simple Stropwatch! travy92 VB Tutorials 19 05-04-2009 05:02 AM
implementing a simple database martybell Visual Basic Programming 3 11-06-2007 05:25 PM
Banner ads DevilsCharm Marketing 7 07-06-2006 12:33 PM


All times are GMT -5. The time now is 08:16 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0