Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-10-2009, 01:26 PM   PM User | #1
najkiie
Regular Coder

 
najkiie's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 103
Thanks: 7
Thanked 0 Times in 0 Posts
najkiie is an unknown quantity at this point
Question Load random line from a textfile - Fatal error?

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..
Code:
<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:
Code:
          strand ((double)microtime()*10000000);
If anyone can tell me what i am doing wrong, it'd be greatly appreciated.

Thanks in advance.
-Nike
__________________
-Nike - PTS - Blubbz
I would love to change the world, but they won't give me the source code.

Last edited by najkiie; 05-10-2009 at 01:28 PM..
najkiie is offline   Reply With Quote
Old 05-10-2009, 01:33 PM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
strand is not a function. srand is.
venegal is offline   Reply With Quote
Old 05-10-2009, 01:46 PM   PM User | #3
najkiie
Regular Coder

 
najkiie's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 103
Thanks: 7
Thanked 0 Times in 0 Posts
najkiie is an unknown quantity at this point
ook.

I got the code from this site:
http://www.trap17.com/index.php/Php-...le_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?
__________________
-Nike - PTS - Blubbz
I would love to change the world, but they won't give me the source code.
najkiie is offline   Reply With Quote
Old 05-10-2009, 02:23 PM   PM User | #4
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
Quote:
Originally Posted by najkiie View Post
ook.

I got the code from this site:
http://www.trap17.com/index.php/Php-...le_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?
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 05-10-2009, 02:28 PM   PM User | #5
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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,
PHP Code:
$f_contents file("quotes.txt");
$line $f_contents[array_rand($f_contents)];
echo 
$line
will work just fine.
venegal is offline   Reply With Quote
Old 05-10-2009, 02:30 PM   PM User | #6
najkiie
Regular Coder

 
najkiie's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 103
Thanks: 7
Thanked 0 Times in 0 Posts
najkiie is an unknown quantity at this point
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?
__________________
-Nike - PTS - Blubbz
I would love to change the world, but they won't give me the source code.
najkiie is offline   Reply With Quote
Old 05-10-2009, 02:35 PM   PM User | #7
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
Quote:
Originally Posted by najkiie View Post
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
weir-07 is offline   Reply With Quote
Old 05-10-2009, 02:36 PM   PM User | #8
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Well don't give us such a hard time helping you. Post the whole code and show us which line is generating the error.
venegal is offline   Reply With Quote
Old 05-10-2009, 02:37 PM   PM User | #9
najkiie
Regular Coder

 
najkiie's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 103
Thanks: 7
Thanked 0 Times in 0 Posts
najkiie is an unknown quantity at this point
didn't i say that in the first post?

Alright:

Code:
<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>
__________________
-Nike - PTS - Blubbz
I would love to change the world, but they won't give me the source code.

Last edited by najkiie; 05-10-2009 at 02:42 PM..
najkiie is offline   Reply With Quote
Old 05-10-2009, 02:39 PM   PM User | #10
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
Strange. try commenting out that line and printing the array

PHP Code:
$f_contents file("quotes.txt");
print_r($f_contents);
//$line = $f_contents[array_rand($f_contents)];
//echo $line; 
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 05-10-2009, 02:40 PM   PM User | #11
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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.
venegal is offline   Reply With Quote
Old 05-10-2009, 02:40 PM   PM User | #12
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
Quote:
Originally Posted by najkiie View Post
.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

PHP Code:
srand ((double)microtime()*10000000); 
completely
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 05-10-2009, 02:48 PM   PM User | #13
najkiie
Regular Coder

 
najkiie's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 103
Thanks: 7
Thanked 0 Times in 0 Posts
najkiie is an unknown quantity at this point
Quote:
Originally Posted by timgolding View Post
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

PHP Code:
srand ((double)microtime()*10000000); 
completely

If i remove that line it'll look like this:
PHP Code:
<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]: 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]: 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]: failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\themes\blubbz\footer.php on line 5
__________________
-Nike - PTS - Blubbz
I would love to change the world, but they won't give me the source code.

Last edited by najkiie; 05-10-2009 at 02:51 PM..
najkiie is offline   Reply With Quote
Old 05-10-2009, 02:53 PM   PM User | #14
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
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.

PHP Code:
 $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.
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 05-10-2009, 03:01 PM   PM User | #15
najkiie
Regular Coder

 
najkiie's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 103
Thanks: 7
Thanked 0 Times in 0 Posts
najkiie is an unknown quantity at this point
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.
__________________
-Nike - PTS - Blubbz
I would love to change the world, but they won't give me the source code.
najkiie is offline   Reply With Quote
Reply

Bookmarks

Tags
file, include, random, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:27 PM.


Advertisement
Log in to turn off these ads.