Current time: 11-20-2008, 02:16 PM Hello There, Guest! (LoginRegister)
Quick Login:


Poll: Rate this tutorial
5
4
3
2
1
[Show Results]
 
Post Reply  Post Thread 
Pages (2): « First [1] 2 Next > Last »
[PHP] Random Affiliates
Author Message
Xiao
Administrator
*******
Administrators

Posts: 2,988
Group: Administrators
Joined: Dec 2005
Status: Offline
Reputation: 1
Post: #1
[PHP] Random Affiliates

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:

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

Code:
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 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!"); 

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:

PHP Code:
$num_displayed ;

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

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

$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 Smile2




I'm the only one around here that can ban people, what else do you need to know? Unsure

This post was last modified: 04-12-2006 09:35 PM by Ferali.

03-25-2006 11:32 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Xiao
Administrator
*******
Administrators

Posts: 2,988
Group: Administrators
Joined: Dec 2005
Status: Offline
Reputation: 1
Post: #2
RE: [PHP] Random Affiliates

anyone tested it?




I'm the only one around here that can ban people, what else do you need to know? Unsure
04-01-2006 02:55 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Virtual
Senior Member
****


Posts: 384
Group: Active
Joined: Mar 2006
Status: Offline
Reputation: 0
Post: #3
RE: [PHP] Random Affiliates

Nice explained! Smile2
So u can use rand() in mysql syntax -.- didn't know that Smile2
tY



Current Favo Signature :: Clicky
Current C4D Signature ::<3
My Full Signature Tutorial :: Clicky
04-01-2006 03:25 PM
Find all posts by this user Quote this message in a reply
Xiao
Administrator
*******
Administrators

Posts: 2,988
Group: Administrators
Joined: Dec 2005
Status: Offline
Reputation: 1
Post: #4
RE: [PHP] Random Affiliates

guess nobody tested it cus I just noticed I had some major errors in it Laugh
Fixing them right now Happy
you wouldn't even have gotten past the mysql part :p




I'm the only one around here that can ban people, what else do you need to know? Unsure
04-01-2006 03:34 PM
Visit this user's website Find all posts by this user Quote this message in a reply
NavySeal
Junior Member
**


Posts: 1
Group: Registered
Joined: Apr 2006
Status: Offline
Reputation: 0
Post: #5
RE: [PHP] Random Affiliates

yea i just tried it and it doesnt select the mysql database so i think ill use my own php coding instead Dry

04-08-2006 01:13 AM
Find all posts by this user Quote this message in a reply
silent
Junior Member
**


Posts: 3
Group: Registered
Joined: Apr 2006
Status: Offline
Reputation: 0
Post: #6
RE: [PHP] Random Affiliates

you have quite a few errors you should debug it. I have done some for you.

connect to the database using this instead

if ($dbc = mysql_connect('localhost','username','password')) {

#do whateva

print "connected";

} else {

print '<p>Could not connect to mysql becauase:' . mysql_error() . '</p>';

}

04-09-2006 07:32 PM
Find all posts by this user Quote this message in a reply
Xiao
Administrator
*******
Administrators

Posts: 2,988
Group: Administrators
Joined: Dec 2005
Status: Offline
Reputation: 1
Post: #7
RE: [PHP] Random Affiliates

So everyone viewing the forum would see "Connected"
that's stupid...
And that wouldn't even work..
If the server can't connect he stops the script and displays the error, he wouldn't read the "else" part..




I'm the only one around here that can ban people, what else do you need to know? Unsure

This post was last modified: 04-09-2006 08:02 PM by Xiao.

04-09-2006 07:56 PM
Visit this user's website Find all posts by this user Quote this message in a reply
silent
Junior Member
**


Posts: 3
Group: Registered
Joined: Apr 2006
Status: Offline
Reputation: 0
Post: #8
RE: [PHP] Random Affiliates

no no hence the #do anything .. you put your own code there !
or alternativly if your fussy

if ($dbc=@mysql_connect('localhost','user','password')){

if(!@mysql_select_db ('database')){

die('could not select the database because' . mysql_error() . 'added here');

}

} else {

die('could not connect to the database because');
}

}


silent, proud member of GFX-Depot since Apr 2006.

This post was last modified: 04-09-2006 08:02 PM by silent.

04-09-2006 08:01 PM
Find all posts by this user Quote this message in a reply
Xiao
Administrator
*******
Administrators

Posts: 2,988
Group: Administrators
Joined: Dec 2005
Status: Offline
Reputation: 1
Post: #9
RE: [PHP] Random Affiliates

PHP Code:
mysql_connect($host,$user,$password);
@
mysql_select_db($database) or die( "Unable to select database!"); 


displays an error if it fails, continues if it doesn't...




I'm the only one around here that can ban people, what else do you need to know? Unsure
04-09-2006 08:03 PM
Visit this user's website Find all posts by this user Quote this message in a reply
silent
Junior Member
**


Posts: 3
Group: Registered
Joined: Apr 2006
Status: Offline
Reputation: 0
Post: #10
RE: [PHP] Random Affiliates

just give this code a try its slightly more structured

nice site you guys got here btw


silent, proud member of GFX-Depot since Apr 2006.
04-09-2006 08:06 PM
Find all posts by this user Quote this message in a reply
Pages (2): « First [1] 2 Next > Last »
Post Reply  Post Thread 

View printable version View a Printable Version
Send this thread to a friend Send this Thread to a Friend
Subscribe to thread Subscribe to this Thread | Add to favourites Add Thread to Favorites

Forum Jump: