Jump to content

Randomly displaying a list of links


Orionsbelter

Recommended Posts

Ok so i'm wanting to create some PHP to display a list of links in a different order each time a visitor visits the page.

 

For example

list:

link 1

link 2

link 3

link 4

 

I WISH TO RANDOMISE THIS ORDER EACH TIME THE PAGE IS LOADED.

 

so the next time they visit the order may go like this:

 

Link 3

Link 2

Link 4

Link 1

 

If anyone can help i will be very thankful.

 

 

Thank you for reading.

 

Link to comment
Share on other sites

i know a small code it is not of ur requirement but i think this would give u an idea

 


<? 

//set the urls 
$urls = array("http://google.com" ,"http://hotmail.com" ,"http://phpfreecode.com" ); 

//set the text links 
$text = array("Google" ,"Hotmail" ,"phpfreecode"); 
        srand(time()); 

//set the number in (rand()%3); for however many links there are 
        $random = (rand()%3); 
echo ("<a href = \"$urls[$random]\">$text[$random]</a>")."<br>"; 


?>

Link to comment
Share on other sites

i know a small code it is not of ur requirement but i think this would give u an idea

 


<? 

//set the urls 
$urls = array("http://google.com" ,"http://hotmail.com" ,"http://phpfreecode.com" ); 

//set the text links 
$text = array("Google" ,"Hotmail" ,"phpfreecode"); 
        srand(time()); 

//set the number in (rand()%3); for however many links there are 
        $random = (rand()%3); 
echo ("<a href = \"$urls[$random]\">$text[$random]</a>")."<br>"; 


?>

 

I don't see how that will echo all of the links?

 

Straight from PHP.net for associative arrays, keeping the key => value pairs in tact.

 

<?php
function shuffle_assoc($array) {
  $keys = array_keys($array);
  
  // Shuffle the keys
  shuffle($keys);
  
  // Re-assign each new ordered key and value pair to a new array
  foreach($keys as $key) {
    $new[$key] = $array[$key];
  }
  
  // Return the new array
  return $new;
}

// Array of websites, the URL is the key and the label is the value
$sites = array('http://www.google.com/' => 'Google', 'http://www.phpfreaks.com/' => 'PHP Freaks');

// Loop through each element in the newly ordered array
foreach (shuffle_assoc($sites) as $key => $val) {
  echo '<a href="'. $key .'">'. $val .'</a><br />';
}
?>

 

If you're getting these values from a database then it's best you use the RAND() MySQL function.

Link to comment
Share on other sites

No they will be in the php code and not from a database, thank you to all who have replied so far :D

 

Well in that case, the code I posted before will work if the links are in an associative array. I've tried and tested it and it works fine as far as I'm aware.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.