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. Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/ 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/#findComment-1010032 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? Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/#findComment-1010037 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. Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/#findComment-1010043 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 Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/#findComment-1010051 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. Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/#findComment-1010056 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 Link to comment https://forums.phpfreaks.com/topic/191610-randomly-displaying-a-list-of-links/#findComment-1010061 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.