http://www.rate-my-body.info/me/test1.php is the page with the problem <body> <?PHP $fh = fopen('file.txt','r'); $contents = fread($fh, filesize($file)); $words = explode(" ", $contents); $wordcount = count($words) - 1; $randomnum = mt_rand(0, $wordcount); echo $words[$randomnum]; ?> </body> Code (markup): its saying there is a problem on line 11 which is: $contents = fread($fh, filesize($file)); Any help would be greatly appreciated, the script basically displays a random link each time the page is visited or refreshed, if you need that info
You mention $file which is never intialized: Try: <?PHP $fh = fopen('file.txt','r'); $contents = fread($fh, filesize($fh)); // $file -> $fh $words = explode(" ", $contents); $wordcount = count($words) - 1; $randomnum = mt_rand(0, $wordcount); echo $words[$randomnum]; ?> PHP:
Looks like the file doesn't exist. The code originally probably looked like this, making my suggestion wrong: <?PHP $file = "file.txt"; //Make sure this exists in the same folder with same name! And is readable! $fh = fopen($file,'r'); $contents = fread($fh, filesize($file)); $words = explode(" ", $contents); $wordcount = count($words) - 1; $randomnum = mt_rand(0, $wordcount); echo $words[$randomnum]; ?> PHP:
Replace your code with the following: <?PHP $textfile ="file.txt"; $quotes = file("$textfile"); $quote = rand(0, sizeof($quotes)-1); echo $quotes[$quote]; ?> PHP:
Yes, though note that will print an entire line as oppose to one single word. Edit: Judging by this file: http://www.rate-my-body.info/me/file.txt that's actually what you want, one line. Nice pics Might want to disallow folder listings on your server.
This works but it's kind of nasty to seek at the end and then back to the beginning : <? $fh = fopen('file.txt','r'); fseek($fh, 0, SEEK_END); $fsize = ftell($fh); fseek($fh, 0, SEEK_BEGIN); $contents = fread($fh,$fsize); $words = explode(" ", $contents); $wordcount = count($words) - 1; $randomnum = mt_rand(0, $wordcount); echo $words[$randomnum]; ?> PHP:
Why don't you get the links from a database anyway? The single word one will break because of the explode based on space. That means you can get <a one time and href"etc."> next time. You will want it by line or from a database.
Well, if he puts the links like this in the file.txt it'll display a clickable link just fine: <a href="http://www.domain1.com">link1</a> <a href="http://www.domain2.com">link1</a>
TOPS3O is right, you would have better luck with a database. If you insist on working with a file, make sure you enter the url's one line at a time ( and use \n as the separator instead of space) or in the worst case a character that won't appear in url's such as | , \t (tab)
Yeah I saw that and editing my post accordingly. Unless you actually checked that file, your suggestion was guessed correctly. The word by word one is defintely not the way to go.
cheers everyone its working now, i just have to make the site (tops i dont really mind, not many people will see, my ex asked me to put them up suppose should take them off now really)
just another thought, but this may be getting too complicated for me, if i wanted 2 of the random links would i put that echo line twice? and would this mean at some oint i would be likely to get the same link twice at some point?
You'll also have to create the random number twice then and THEN echo it. Do it like this to avoid the same twice: <?PHP $textfile ="file.txt"; $quotes = file("$textfile"); $quote = rand(0, sizeof($quotes)-1); echo '<br>First Link: ' . $quotes[$quote]; $newquote = rand(0, sizeof($quotes)-1); if($newquote != $quote) { echo '<br>Second (Unique) Link: ' . $quotes[$newquote]; } else { if (isset($quotes[$newquote+1])){ $newquote = $newquote + 1; } else { $newquote = $newquote - 1; } echo '<br>Second (Unique) Link: ' . $quotes[$newquote]; } ?> PHP: That picks the next one up or down if it happens to pick the same one again.