Sign in to follow this  
I Like Bread

[web] Different links every load

Recommended Posts

I Like Bread    630
I'm trying to create a somewhat dynamic site where content changes for every visit. I have a banner at the top which displays a couple links, and I'd like those to change-- not in a rolling series of ads kind of thing, but just different every time the page is loaded (or depending on the day of the week). At W3Schools, I found a script that allows me to list a few links, and one is picked at random during load. I can settle for that, but I'd rather have a table of links somewhere that I can edit independently (and possibly put other stuff to access like Flash ads and off-site links). Can anyone point me toward a tutorial that does this?

Share this post


Link to post
Share on other sites
Sander    1332
Are you looking into server-side scripting (PHP/ASP) or into client-side scripting? If the latter then you're basically stuck with your list of links and pick one at random with javascript.

Share this post


Link to post
Share on other sites
Sander    1332
Well, assuming you use PHP (if not, it's almost identical in other languages) you would get something like this:

<?php
$list = file_get_contents('/path/to/your/links.txt');
$list = explode("\n", $list);
$link = array_rand($list);
echo '<a href="$link">$link</a>';
?>




Or, if you prefer a database (e.g. table "links" with fields "link" and "description"):

<?php
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name');

$list = array();
$i = 0;
$result = mysql_query('SELECT * FROM links');
while( $row = mysql_fetch_assoc($result) )
{
$list[$i]['link'] = $row['link'];
$list[$i]['desc'] = $row['description'];
$i++;
}

$link = array_rand($list);
echo '<a href="$link[link]">$link[desc]</a>';

mysql_free_result($result);
mysql_close($conn);
?>


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this