Orionsbelter Posted February 10, 2010 Share Posted February 10, 2010 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. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 10, 2010 Share Posted February 10, 2010 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>"; ?> Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 10, 2010 Share Posted February 10, 2010 Are the links in the page, or are they being extracted from a database? Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 10, 2010 Share Posted February 10, 2010 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. Quote Link to comment Share on other sites More sharing options...
Orionsbelter Posted February 10, 2010 Author Share Posted February 10, 2010 No they will be in the php code and not from a database, thank you to all who have replied so far Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 10, 2010 Share Posted February 10, 2010 No they will be in the php code and not from a database, thank you to all who have replied so far 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. Quote Link to comment Share on other sites More sharing options...
Orionsbelter Posted February 10, 2010 Author Share Posted February 10, 2010 Ok thank you for all your help i'll test it now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.