Thread: I'm lost

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7

    I'm lost


    I want to read a line from a text file into an array, then count the number of array items and store it in a counter.

    Then I want to randomly display a line on the page.

    I know I should post at least a try but I really dont know where to begin. Thanks .
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,110
    Rep Power
    9398
    file() to grab the lines from the file, count() to get the number of lines, and array_rand() to pick a random line number.
    Last edited by ManiacDan; October 12th, 2012 at 09:49 PM. Reason: files->file
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    Something like this?

    PHP Code:
    <?php

    $lines 
    file('proverbs.txt');
    $ProverbCount count($lines);
    echo 
    rand(0$ProverbCount 1);
    ?>
    throws an error though.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    What error?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    Parse error: syntax error, unexpected T_IF in F:\EasyPHP-5.3.2i\www\test.php on line 56

    There isn't a line 56?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    ok, i got it but there is one problem.

    here is my working code kind of
    PHP Code:
    <?php

    $lines 
    file("proverbs.txt");
    $ProverbCount count($lines);
    echo 
    rand(0$ProverbCount-1);
    ?>
    Right now it just shows the index #. These are actually lines of text. How can I modify it to show the text and not the index #?
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,110
    Rep Power
    9398

IMN logo majestic logo threadwatch logo seochat tools logo