Web Analytics Made Easy -
StatCounter Pulling Text From A Text File. - CodingForum

Announcement

Collapse
No announcement yet.

Pulling Text From A Text File.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pulling Text From A Text File.

    I have a script that will randomly pull text out of a file, replace a chosen word with whatever the user inputs, and displays one line out of the text file, for some reason, since i ported it over to my new site, it's displaying everything that's inside the text file instead of just the one line.

    Can anyone see where i've gone wrong?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
    <title>Fact Generator</title>
    
    </head>
    
    
    
    <body>
    
    <div align="center">
    
      <p>&nbsp;  </p>
    
      <p>
    
    <?php
    
    if(isset($_POST['fname'])) {
    
        $fname = $_POST['fname'];
    
        $filename = "facts.php";
    
        $filedata = file($filename);
    
        shuffle($filedata);
    
        $farray = str_replace(fname, $fname, $filedata[array_rand($filedata)]);
    
        foreach($filedata as $farray) {
    
            echo wordwrap($farray,50,"<br/>");    }
    
    }
    
    ?>
    
        </p>
    
        </div>
    
      </p>
    
    </div>
    
    <p align="center">&nbsp;</p>
    
      <p align="center">&nbsp;</p>
    
      <p align="center">&nbsp;</p>
    
    <p align="center" class="style4">Enter your name: </p>
    
    <p align="center">
    
    <form action="factgen.php" method="post" target="_self" />
    
    
    
      <input type="text" size="30" name="fname" value="<?php echo $fname; ?>"/> 
    
      </p>
    
    <p align="center">
    
    <input type="submit" value="Fact!" /></p>
    
    <p align="center">&nbsp;</p>
    
    
    
    
    
    </body>
    </html>

  • #2
    PHP Code:
    $filedata explode('\n'file($filename)); 

    Comment


    • #3
      you still need to read the content before you can out put it....

      Comment


      • #4
        Originally posted by TheHaloEffect View Post
        $filedata = file($filename);
        He has...

        Comment


        • #5
          I now get
          Warning: Invalid argument supplied for foreach() in /home/hardware/public_html/random/factgen.php on line 20

          When I use explode???

          Comment


          • #6
            No idea anyone?

            Comment


            • #7
              tried implode()?

              Comment


              • #8
                I'm at college right now, I'll try it when I get home, been trying to find the web design tutor to help, but i can't find him anywhere =/

                Comment


                • #9
                  with $filedata = implode('\n', file($filename));

                  I get the error: -



                  Warning: shuffle() expects parameter 1 to be array, string given in /home/hardware/public_html/random/factgen.php on line 17

                  Warning: array_rand() [function.array-rand]: First argument has to be an array in /home/hardware/public_html/random/factgen.php on line 18

                  Warning: Invalid argument supplied for foreach() in /home/hardware/public_html/random/factgen.php on line 19




                  it's really starting to bug me now.

                  Comment

                  Working...
                  X