daily-random-banner

Gesponsorde koppelingen

PHP script bestanden

  1. daily-random-banner

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/***********************************************************
Daily (random) banner ver. 1.0
This simple banner system will show a different banner each day

Copyright (C) 2005 - Olaf Lederer

************************************************************/


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 IF NOT EXISTS `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>";
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.