WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
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 Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
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
Submit Site
Forex Trading Online forex trading platform

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 : Daily (random) banner
Categories : PHP, MySQL, Databases Click here to Update Your Picture
Olaf Lederer
Date : Feb 02nd 2005
Grade : 3 of 5 (graded 6 times)
Viewed : 3515
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Olaf Lederer
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This banner rotator is a little bit different from others, the banner will change only ones a day. I used the php function rand() because I think that MySQL's "ORDER BY RAND()" and the php function array_rand() are only a second choice.

<?php
mysql_connect
("localhost", "user", "password");
mysql_select_db("database_name");

// create table for the banners
mysql_query("
CREATE TABLE IF NOT EXISTS 'banners' (
  'id' int(11) NOT NULL auto_increment,
  'name' varchar(35) NOT NULL default '',
  'link' varchar(150) NOT NULL default '',
  'alt_title' varchar(100) NOT NULL default '',
  'image' varchar(35) NOT NULL default '',
  'ins_date' date NOT NULL default '0000-00-00',
  'status' enum('on','off') NOT NULL default 'on',
  PRIMARY KEY  ('id')
) TYPE=MyISAM AUTO_INCREMENT=1"
);

// create the table for storing todays banner id
mysql_query("
CREATE TABLE 'rand_banner' (
  'id' int(11) NOT NULL auto_increment,
  'date' date NOT NULL default '0000-00-00',
  'todays_id' int(11) NOT NULL default '0',
  PRIMARY KEY  ('id')
) TYPE=MyISAM AUTO_INCREMENT=1"
);

// first check if todays banner id is already stored
$check_today_sql = "SELECT todays_id FROM rand_banner WHERE date = NOW()";
$check_today_res = mysql_query($check_today_sql);

If (
mysql_num_rows($check_today_res) > 0) {
   
$id_today = mysql_result($check_today_res, 0, "todays_id");
}Else{
   
// if not select a random id and store the id in the table with current date
   
$get_ids_sql = "SELECT id FROM banners WHERE status = 'on'";
   
$get_ids_res = mysql_query($get_ids_sql);
   
$get_ids_array = array();
   
    while (
$get_ids = mysql_fetch_object($get_ids_res)) {
       
$get_ids_array[] = $get_ids->id;
    }
   
$num = count($get_ids_array);
   
// I use the function rand() because other random functions are not "really random"
   
$rand_num = rand(0, $num-1);
   
$id_today = $get_ids_array[$rand_num];
   
mysql_query("INSERT INTO rand_banner (id, date, todays_id) VALUES (NULL, NOW(), $id_today)");
}
// at least select the record with the todays ID
$result = mysql_query("SELECT link AS url, alt_title, image FROM banners WHERE id = $id_today");
$obj = mysql_fetch_object($result);

// this example will show the current banner
echo "<a href=\"".$obj->url."\"><img src=\"/images/banners/".$obj->image."\" alt=\"".$obj->alt_title."\" border=\"0\"></a>";
?>



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
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
Alternating background color for HTML table rows
Categories : PHP, Databases, MySQL, HTML and PHP
color codes for positive and negative numbers
Categories : PHP, MySQL, Databases, HTML
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Linked comboboxes with php-mysql & javascript
Categories : PHP, Java Script, Databases, MySQL
mysql_escape_string
Categories : PHP, MySQL, Databases, Strings
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
This script is a contact form between users of a website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps