luke777 Posted April 29, 2007 Share Posted April 29, 2007 I need some kind of code in php that can display 10 random links from a big list of links. So i have a file links.php which contains a list of lets say 1000's of links, how can i grab just 10 RANDOM links from that list to display on a webpage. Please help me ! Thanks Luke. Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/ Share on other sites More sharing options...
AndyB Posted April 29, 2007 Share Posted April 29, 2007 Would have been a nice idea to have had the links in a database. Then this would have been trivial. Does links.php contain anything except thousands of lines each one of which is a link? What's its structure? Post a few lines of it. Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/#findComment-241022 Share on other sites More sharing options...
php_joe Posted April 29, 2007 Share Posted April 29, 2007 Try this: <? $lines = file("./link_list.txt"); shuffle($lines); $i = '0'; while($i < 10){ echo "<a href=\"$lines[$i]\">$lines[$i]</a>"; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/#findComment-241126 Share on other sites More sharing options...
luke777 Posted April 29, 2007 Author Share Posted April 29, 2007 Ok I got this working But the only probelm is it sometimes displays the same link twice :S Is there some way of removing duplicates??? Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/#findComment-241239 Share on other sites More sharing options...
AndyB Posted April 29, 2007 Share Posted April 29, 2007 Oh, you mean your original list has duplicates? Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/#findComment-241242 Share on other sites More sharing options...
bobleny Posted April 29, 2007 Share Posted April 29, 2007 Dang while loops! I think you should use this instead... They do the exact same thing though.. lol <?php $lines = file("./link_list.txt"); shuffle($lines); for($i = 0; $i < 10; $i++) { echo "<a href=\"$lines[$i]\">$lines[$i]</a>"; } ?> Also, I believe the only way to get duplicates, is if you have duplicates in the file.... Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/#findComment-241256 Share on other sites More sharing options...
luke777 Posted April 30, 2007 Author Share Posted April 30, 2007 Ok thanks alot for your help, great forum ill be sticking around ! Luke. Link to comment https://forums.phpfreaks.com/topic/49180-generate-random-links-from-big-list-of-links/#findComment-241449 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.