...

View Full Version : Load random line from a textfile - Fatal error?



najkiie
05-10-2009, 01:26 PM
Hey.

I'm trying to insert a random line from a textfile into my website. This is what i have so far, i can't see whats wrong.


I have my footer.php file which i want the text file to be inserted to..


<div id="footer">
<div class="footer-inner">
<div class="random-quote">
<?php
strand ((double)microtime()*10000000);
$f_contents = file ("quotes.txt");
$line = $f_contents[array_rand ($f_contents)];
echo $line;
?>
</div>
</div>
</div>
Is there anything wrong in there?

I also have my quotes.txt in the same directory. And i have one sentence on each line. (about 30 lines)

Here's the error message i get:
Fatal error: Call to undefined function strand() in URL_TO_FOOTER.PHP on line 5

And this is what i have on line 5:

strand ((double)microtime()*10000000);

If anyone can tell me what i am doing wrong, it'd be greatly appreciated.

Thanks in advance.
-Nike

venegal
05-10-2009, 01:33 PM
strand is not a function. srand is.

najkiie
05-10-2009, 01:46 PM
ook.

I got the code from this site:
http://www.trap17.com/index.php/Php-Fetching-Random-Line-Text-File_t62062.html

I tried adding a dot but it still isn't working. I have a feeling i aint to far away from a solution, can you help me?

timgolding
05-10-2009, 02:23 PM
ook.

I got the code from this site:
http://www.trap17.com/index.php/Php-Fetching-Random-Line-Text-File_t62062.html

I tried adding a dot but it still isn't working. I have a feeling i aint to far away from a solution, can you help me?

But did you try changing strand to srand?

venegal
05-10-2009, 02:28 PM
You did *what*? Where did you add a dot? And how is adding a dot somewhere supposed to fix the code, when all you have to do is change strand to srand?

Anyway, since PHP 4.2 you don't have to use srand any more,

$f_contents = file("quotes.txt");
$line = $f_contents[array_rand($f_contents)];
echo $line;

will work just fine.

najkiie
05-10-2009, 02:30 PM
No i just added a dot before (sorry, i read wrong).

I tried now and i got this error this time:
Parse error: parse error in URL_TO_THE_FOOTER.PHP on line 5

it's better than a Fatal error, right? :)

weir-07
05-10-2009, 02:35 PM
No i just added a dot before (sorry, i read wrong).

I tried now and i got this error this time:
Parse error: parse error in URL_TO_THE_FOOTER.PHP on line 5

it's better than a Fatal error, right? :)

Whats the code on Line 5 ? :P

venegal
05-10-2009, 02:36 PM
Well don't give us such a hard time helping you. Post the whole code and show us which line is generating the error.

najkiie
05-10-2009, 02:37 PM
didn't i say that in the first post?

Alright:



<div id="footer">
<div class="footer-inner">
<div class="random-quote">
<?php
srand ((double)microtime()*10000000); /* THIS LINE IS CAUSING THE ERROR*/
$f_contents = file ("quotes.txt");
$line = $f_contents[array_rand ($f_contents)];
echo $line;
?>
</div>


</div> <!-- End footer-inner -->
</div> <!-- End footer -->

</body>
</html>

timgolding
05-10-2009, 02:39 PM
Strange. try commenting out that line and printing the array



$f_contents = file("quotes.txt");
print_r($f_contents);
//$line = $f_contents[array_rand($f_contents)];
//echo $line;

venegal
05-10-2009, 02:40 PM
Do you even read what I write? Lose the dot, or better yet lose the whole line, it's not necessary any more. This is my last post in this thread.

timgolding
05-10-2009, 02:40 PM
.srand ((double)microtime()*10000000);

As venegal already stated you don't need that line anymore and its still a mistery why you put a dot infront there. SO just remove



srand ((double)microtime()*10000000);


completely

najkiie
05-10-2009, 02:48 PM
As venegal already stated you don't need that line anymore and its still a mistery why you put a dot infront there. SO just remove



srand ((double)microtime()*10000000);
completely


If i remove that line it'll look like this:


<div id="footer">
<div class="footer-inner">
<div class="random-quote">
<?php
$f_contents = file ("quotes.txt"); /*Line 5*/
$line = $f_contents[array_rand ($f_contents)]; /*Line 6*/
echo $line;
?>
</div>




</div> <!-- End footer-inner -->
</div> <!-- End footer -->

</body>
</html>
And when i view it in the browser i get this error:

Warning: file(quotes.txt) [function.file (http://localhost/wordpress/function.file)]: failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\themes\blubbz\footer.php on line 5

Warning: array_rand() [function.array-rand (http://localhost/wordpress/function.array-rand)]: First argument has to be an array in C:\xampp\htdocs\wordpress\wp-content\themes\blubbz\footer.php on line 6


And if i comment it out and print the array as you said above i get this:
Warning: file(quotes.txt) [function.file (http://localhost/wordpress/function.file)]: failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\themes\blubbz\footer.php on line 5

timgolding
05-10-2009, 02:53 PM
Those error messages are telling you that it can't read the file quotes.txt

Where does quotes.txt live. You have to tell the file() function where it is to be able to read it.



$f_contents = file ("quotes.txt");


This is looking for a file called quotes.txt in the same directory as the file that the script is in. SO maybe the script and the file are in different locations. Can you tell us where the two files are. 1) the script, 2) The quotes.txt file

The second error message is only there because the first one is. If the function file was successful and returned an array it would store that in $f_contents so the line 6 would have been fine. But it didnt and returned a boolean false so it failed too.

najkiie
05-10-2009, 03:01 PM
The quotes.txt is in the same directory as the script is in.

-Website Folder
--Header.php
--Index.php
--Footer.php <- The script i'm working on..
--quotes.txt <- The file containing the quotes...

And then i have a few other php files, and an image directory with the images.

timgolding
05-10-2009, 03:10 PM
Try this.



<div id="footer">
<div class="footer-inner">
<div class="random-quote">
<?php
$f_contents = file ("quotes.txt", FILE_USE_INCLUDE_PATH); /*Line 5*/
$line = $f_contents[array_rand ($f_contents)]; /*Line 6*/
echo $line;
?>
</div>




</div> <!-- End footer-inner -->
</div> <!-- End footer -->

</body>
</html>

najkiie
05-10-2009, 03:21 PM
Try this.



<div id="footer">
<div class="footer-inner">
<div class="random-quote">
<?php
$f_contents = file ("quotes.txt", FILE_USE_INCLUDE_PATH); /*Line 5*/
$line = $f_contents[array_rand ($f_contents)]; /*Line 6*/
echo $line;
?>
</div>




</div> <!-- End footer-inner -->
</div> <!-- End footer -->

</body>
</html>



It worked :O

Did you only add ", FILE_USE_INCLUDE_PATH" to it? Was that all?

Thanks a lot dude. Really appreciated. :)

timgolding
05-10-2009, 03:27 PM
Yeah I just added that because although the file Footer.php was in the same folder the script that was running was probably a different file that included Footer.php as an include. So using the flag FILE_USE_INCLUDE_PATH meant that it finds the file relative to the include and not the script itself.

najkiie
05-10-2009, 03:36 PM
Yeah I just added that because although the file Footer.php was in the same folder the script that was running was probably a different file that included Footer.php as an include. So using the flag FILE_USE_INCLUDE_PATH meant that it finds the file relative to the include and not the script itself.

That's great. Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum