Page 1 of 1

Random Links

Posted: Tue May 26, 2009 7:43 pm
by Tsongkie
Ok I found this real cool and simple php code here

Code: Select all

$links = file("links.txt");
echo $links[rand(0,sizeof($links)-1)];
 
As I understand this only shows 1 random link. I want to show 10-20 random links from links.txt. Anyone can point me to the proper direction? :)

Re: Random Links

Posted: Tue May 26, 2009 9:33 pm
by Zoxive
http://www.php.net/for
http://www.php.net/while
http://www.php.net/in_array

Code: Select all

for loop // loop how many links we want, ie 10
   while loop  // check for links already selected, compare with in_array(returned array + current randow row) & check if variable isnt empty (random row, so it will make one)
     select random row set as variable
   Set selected link to returned array

Re: Random Links

Posted: Tue May 26, 2009 10:12 pm
by Benjamin
:arrow: Moved to PHP - Code

Re: Random Links

Posted: Wed May 27, 2009 2:39 am
by Tsongkie
Anyone have a working code for this? I'm sorry i cant seem to get it to work.

Re: Random Links

Posted: Wed May 27, 2009 3:31 am
by onion2k
This is not a forum where you can just ask people to write code for you for free. Post your code and we'll guide you, but we won't write it for you.

Re: Random Links

Posted: Wed May 27, 2009 7:21 am
by mikemike
Personally I'd open the file (http://php.net/fopen) and then read it as I need to using fread, from there you can cut it up line by line using explode or something. This probably isn't the most efficient way to do it but should be fine so long as the file is only a few lines and it's a good place to start.