Jump to content
  • Advertisement
Sign in to follow this  
I Like Bread

[web] Different links every load

This topic is 4876 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!