php code help - simple

Discussion in 'PHP' started by tony84, Feb 13, 2006.

  1. #1
    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 :)
     
    tony84, Feb 13, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    T0PS3O, Feb 13, 2006 IP
    mariush likes this.
  3. tony84

    tony84 Well-Known Member

    Messages:
    1,864
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    140
    #3
    its still coming back with errors
     
    tony84, Feb 13, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    T0PS3O, Feb 13, 2006 IP
  5. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Replace your code with the following:
    
    <?PHP
    $textfile ="file.txt";
    $quotes = file("$textfile");
    $quote = rand(0, sizeof($quotes)-1);
    echo $quotes[$quote];
    ?>
    
    PHP:
    :)
     
    franck~, Feb 13, 2006 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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 :D Might want to disallow folder listings on your server.
     
    T0PS3O, Feb 13, 2006 IP
  7. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    mariush, Feb 13, 2006 IP
    tony84 likes this.
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    T0PS3O, Feb 13, 2006 IP
  9. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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>
    :)
     
    franck~, Feb 13, 2006 IP
  10. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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)
     
    mariush, Feb 13, 2006 IP
  11. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    T0PS3O, Feb 13, 2006 IP
    tony84 likes this.
  12. tony84

    tony84 Well-Known Member

    Messages:
    1,864
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    140
    #12
    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)
     
    tony84, Feb 13, 2006 IP
  13. tony84

    tony84 Well-Known Member

    Messages:
    1,864
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    140
    #13
    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?
     
    tony84, Feb 13, 2006 IP
  14. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.
     
    T0PS3O, Feb 13, 2006 IP
  15. tony84

    tony84 Well-Known Member

    Messages:
    1,864
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    140
    #15
    ooo, cheers for that
     
    tony84, Feb 13, 2006 IP