Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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
    Last edited by najkiie; 05-10-2009 at 01:28 PM.
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    strand is not a function. srand is.

  • #3
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • #6
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    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

  • #8
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Well don't give us such a hard time helping you. Post the whole code and show us which line is generating the error.

  • #9
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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>
    Last edited by najkiie; 05-10-2009 at 02:42 PM.
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #10
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

  • #11
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • #12
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

  • #13
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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
    Last edited by najkiie; 05-10-2009 at 02:51 PM.
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #14
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

  • #15
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •