Right now I'm going to show you a really easy tutorial on how to display X random affiliates on your website.
This script is great for 88*31 buttons, but it can work with any other type of image-links.
What you need:
- a webhost that supports PHP and Mysql
So let's get started.
First of all we have to create a database table.
You can run this query in phpmyadmin:
CREATE TABLE `affiliates` (
`id` int(10) NOT NULL auto_increment,
`url` text NOT NULL,
`image` longtext NOT NULL,
`title` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
This means we create a new table in your database named affiliates.
In this table we insert 4 rows: id, url, image and title.
id will be our PRIMARY KEY, which means this will always be unique for every affiliate. We use auto_increment to let mysql automaticly give the id when no value has been entered.
Now we'll have to insert some affiliates.
Run this sql query:
insert into affiliates values ('',"http://www.affiliate1.com", "image1.gif", "Affiliate1.com");
insert into affiliates values ('',"http://www.affiliate2.com", "image2.gif", "Free tutorials");
insert into affiliates values ('',"http://www.affiliate3.com", "image3.gif", "Photoshop help");
Now create a new file called affiliates.php
We'll start with connecting to the database:
<?php
$user="username";
$password="password";
$database="database";
$host="localhost";
mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database!");
Here you'll have to change "username", "password" and "database" to your login data. You might have to change localhost to your host's databese name, but if your host hasn't mentioned anything, localhost will probably work.
mysql_connect($localhost,$user,$password); means we connect to the database with the variables we declared above.
@mysql_select_db($database) or die( "Unable to select database!"); means we connect to the databse and if it fails, we display an error.
Now 2 add more lines:
$num_displayed = X ;
$result = mysql_query ("SELECT * FROM affiliates ORDER BY RAND() LIMIT $num_displayed");
In the first line you have to replace X with the number of affiliates you want to display.
In the second line we give $result the value of mysql_query ("SELECT * FROM affiliates ORDER BY RAND() LIMIT $num_displayed");
Which means we SELECT all rows (*) FROM our table (affiliates), ORDER them BY RAND()om and LIMIT to X ($num_displayed) results.
Now let's finaly display them:
while ($row = mysql_fetch_array($result))
{
echo "<a href=" . $row["url"] . ">
<img src=" . $row["image"] . " border=0 alt='' title='" . $row["title"] . "'>
</a>" . " ";
}
?>
In short:
As long as there's data in our selection we display our data one by one as a linked image.
That's about it.
Here's the full php code:
<?php
$user="username";
$password="password";
$database="database";
$host="localhost";
mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database!");
$num_displayed = X ;
$result = mysql_query ("SELECT * FROM affiliates ORDER BY RAND() LIMIT $num_displayed");
while ($row = mysql_fetch_array($result))
{
echo "<a href=" . $row["url"] . ">
<img src=" . $row["image"] . " border=0 alt='' title='" . $row["title"] . "'>
</a>" . " ";
}
?>
If you have any problems, feel free to post them below.
I'm not that good with PHP yet, but I hope I know enough to answer your questions