WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Random Banner Rotation with PHP4 and MySQL
Categories : PHP, MySQL, Arrays, Databases Update Picture
Anton Suryawan
Date : May 12th 2002
Grade : 4 of 5 (graded 1 times)
Viewed : 4732
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Anton Suryawan
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?
/*
Random Banner Rotation with PHP4 and MySQL
-------
Author: Anton Suryawan (heads103@flashmail.com)

After I looked at "Automatic banner rotation. Randomly selects a banner for display
by Anton
Olsen(aolsen@graphweb.com)"
on http://www.weberdev.com/get_example.php3?count=400,
I tried to create my own banner with PHP4 and MySQL. This is just a test. Did not
test it yet
with
heavy traffic websites. But the result is sometime the random number _is_not_ really
a
random.

This is the Database Sample:
---------------------------------------------------
        CREATE TABLE `tbl_d_banner` (
                `bannerid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                `src` VARCHAR(128) NOT NULL DEFAULT '',
                `href` VARCHAR(128) NOT NULL DEFAULT '',
                `alt` VARCHAR(128) DEFAULT NULL,
                `typeid` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1',
                PRIMARY KEY (`bannerid`),
                UNIQUE KEY `src` (`src`),
                KEY `href` (`href`,`typeid`)
        ) TYPE=MyISAM;

        INSERT INTO tbl_d_banner VALUES
("1","image/bigbanner01.jpg","http://big.company1.com","Text For Big Banner 1","1");
        INSERT INTO tbl_d_banner VALUES
("2","image/bigbanner02.jpg","http://big.company2.com","Text For Big Banner 2","1");
        INSERT INTO tbl_d_banner VALUES
("3","image/bigbanner03.jpg","http://big.company3.com","Text For Big Banner 3","1");
        INSERT INTO tbl_d_banner VALUES
("4","image/bigbanner04.jpg","http://big.company4.com","Text For Big Banner 4","1");
        INSERT INTO tbl_d_banner VALUES
("5","image/bigbanner05.jpg","http://big.company5.com","Text For Big Banner 5","1");
        INSERT INTO tbl_d_banner VALUES
("6","image/smallbanner01.jpg","http://small.company1.com","Text For Small Banner
1","2");
        INSERT INTO tbl_d_banner VALUES
("7","image/smallbanner02.jpg","http://small.company2.com","Text For Small Banner
2","2");
        INSERT INTO tbl_d_banner VALUES
("8","image/smallbanner03.jpg","http://small.company3.com","Text For Small Banner
3","2");
        INSERT INTO tbl_d_banner VALUES
("9","image/smallbanner04.jpg","http://small.company4.com","Text For Small Banner
4","2");
        INSERT INTO tbl_d_banner VALUES
("10","image/smallbanner05.jpg","http://small.company5.com","Text For Small Banner
5","2");


        CREATE TABLE `tbl_d_bannertype` (
                `typeid` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
                `imgwidth` INT(10) UNSIGNED NOT NULL DEFAULT '0',
                `imgheight` INT(10) UNSIGNED NOT NULL DEFAULT '0',
                `border` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
                `typename` VARCHAR(16) NOT NULL DEFAULT '',
                PRIMARY KEY (`typeid`),
                UNIQUE KEY `typename` (`typename`),
                KEY `typename_2` (`typename`)
        ) TYPE=MyISAM;


        INSERT INTO tbl_d_bannertype VALUES("1","468","60","0","big");
        INSERT INTO tbl_d_bannertype VALUES("2","120","120","0","small");
---------------------------------------------------

Does anyone want to port this become a Class?
*/
?>

<?
$sHost = "localhost";
$sDBUser = "myuser";
$sDBPassw = "mypassword";
$sDatabase = "mybanner";

function GetBanner($bannertype) {
global $sHost,$sDBUser,$sDBPassw,$sDatabase;

settype($aBanner,"array");

$bannertype = strtolower($bannertype);

$dbconn = mysql_connect($sHost,$sDBUser,$sDBPassw) or die("xx:Server Unavailable");
mysql_select_db($sDatabase) or die("xx:Database Unavailable");

$strSQL = "SELECT b.bannerid ".
"FROM tbl_d_banner=b, tbl_d_bannertype=t ".
"WHERE b.typeid=t.typeid AND ".
"t.typename='$bannertype';";

$result = mysql_query($strSQL,$dbconn) or die("xx:Query Unavailable");

while ($rs = mysql_fetch_array($result)) {
$aBanner[] = $rs["bannerid"];
};

return $aBanner;

mysql_free_result($result);
mysql_close($dbconn);
};

function SetBanner($bannerid) {
global $sHost,$sDBUser,$sDBPassw,$sDatabase;

settype($sBanner,"string");

$dbconn = mysql_connect($sHost,$sDBUser,$sDBPassw) or die("zz:Server Unavailable");
mysql_select_db($sDatabase) or die("zz:Database Unavailable");

$strSQL = "SELECT b.src, b.href, b.alt, t.imgwidth, t.imgheight, t.border ".
"FROM tbl_d_banner=b, tbl_d_bannertype=t ".
"WHERE t.typeid=b.typeid AND ".
"b.bannerid=$bannerid;";

$result = mysql_query($strSQL,$dbconn) or die("zz:Query Unavailable");

$rs = mysql_fetch_array($result);

$sBanner = "<a href=\"".$rs["href"]."\">".
"<img src=\"".$rs["src"]."\" alt=\"".$rs["alt"]."\" width=\"".$rs
["imgwidth"]."\"
height=\"".$rs["imgheight"]."\" border=\"".$rs["border"]."\">".
"</a>";

return $sBanner;

mysql_free_result($result);
mysql_close($dbconn);
};

function Banner($bannertype) {
settype($aLoadBanner,"array");

$aLoadBanner = GetBanner($bannertype);
$iCountBanner = count($aLoadBanner) - 1;

$x = rand(0,$iCountBanner);

return SetBanner($aLoadBanner[$x]);
}
?>

<!-- Output Here -->

<html>
<head><title>Banner Test</title></head>

<body>
<table border=1 cellpadding=0 cellspacing=2 width="600">

<!-- top banner -->
<tr><td colspan=2 align=center valign=middle><?= Banner("BIG"); ?></td></tr>

<!-- left banner -->
<tr><td align=center valign=top width="140"><?= Banner("small"); ?></td>

<td align=center valign=top width="100%">This is just a test...</td></tr>
</table>
</body>
</html>



This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query
Categories : Databases, PHP, MySQL, Arrays
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Function for retrieving MySQL enum values into a PHP array.
Categories : PHP, Databases, MySQL, Arrays
Finds the median in an array of numbers - Can be used with a MySql database column read into an array
Categories : PHP, Arrays, Databases, MySQL
create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses a 'multi-dimension array', but not really as the array is just one big array with the index of "[$i][$j]". Have a look at the code and you'll see what I mean.
Categories : PHP, MySQL, Arrays, Databases
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, MySQL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
Multiple Search using PHP and Mysql
Categories : PHP, Databases, General SQL, MySQL
mysql_escape_string
Categories : PHP, MySQL, Databases, Strings
myCSV-dump converts a simple CSV-flatfile-database into an MySQL-dump.
Categories : PHP, MySQL, Databases, Complete Programs