Hi guys, I'm kind new to the whole php thing.

Currently got a website that has a news page and an admin page. in the admin page there is a text area and a submit button.
CLick the submit button and it updates a file called headline.txt with whatever was in the textarea and consequently updates the news page.

All very basic. However, when I go back to the admin page, only the first line of the headline.txt is called back.

Code for the admin input form:

<form action="update.php" method="GET">
<?
$prevalue = file("./headline.txt");
?>
<textarea name="headline" style="width:100%; height:600px"><? echo $prevalue[0]; ?></textarea>
<input type="submit" value="Update Headline">
</form>

I assume its something wrong this side as the news page updates all the text, not just 1 line.

Any ideas? Need more info?

    Use [man]file_get_contents/man instead of [man]file/man and output $prevalue instead of $prevalue[0]. Or, if that's all you need the contents of that file for, just [man]readfile/man it where you need it:

    <textarea name="headline" style="width:100%; height:600px"><?php readfile("./headline.txt"); ?></textarea>
    

    If you really want $prevalue to be an array of lines from that file, then implode it for the output:

    <textarea name="headline" style="width:100%; height:600px"><?php echo implode('', $prevalue); ?></textarea>
    

    PS: You should get into the habit of using "<?php" instead of "<?", for better script portability and for forward compatibility should the short open tags every be removed from future PHP versions.

      Fantastic, thanks.

      Second question;

      When I input stuff and click update, it displays the headline.txt on the news page. However, it doesn't seem to retain any form of formatting. Ie, it all apears on one line.
      Any idea how I can get it to mirror the layout as it was input?

      Here's my update.php code

      <?php
      
      $url = "http://www.portslacrosse.com/";
      
      if (isset($_GET['headline'])) {
      $newsopen = "./headline.txt";
      
      $newshandle = fopen($newsopen, 'w') or die("Oh, it's broken");
      
      $newsdata = stripslashes(urldecode($_GET['headline']));
      
      fwrite($newshandle, $newsdata);
      
      fclose($newshandle);
      
      }
      
      // Go to the home page
      header("Location: $url");
      
      ?>

      My index page simply has

      <?php include("./headline.txt"); ?>

      is it possible to get some kind of formatting back in there without the user having to enter html in the textarea box?

        If it's within a <textarea> or <pre> block, it should appear as it does in the text file. If you want it within another type of element, then you could add a style="white-space: pre" attribute to that element. If you just want to output the text file and nothing else, add a content type header:

        <?php
        header('Content-Type: text/plain');
        include './headline.txt';
        

          Another option would be [man]nl2br/man, which converts regular line breaks into the HTML 'BR' entity. If you view the source of the page, you should see that the line breaks were preserved... it's just that browsers don't look for line breaks, they look for a <br> tag.

            NogDog wrote:

            If it's within a <textarea> or <pre> block, it should appear as it does in the text file. If you want it within another type of element, then you could add a style="white-space: pre" attribute to that element. If you just want to output the text file and nothing else, add a content type header:

            <?php
            header('Content-Type: text/plain');
            include './headline.txt';
            

            I've put that in the index page but now I'm getting the error:

            Warning: Cannot modify header information - headers already sent by (output started at /home/.wyla/calibretto/portslacrosse.com/index.php:7) in /home/.wyla/calibretto/portslacrosse.com/index.php on line 82

            The output is just straight into a table cell, not a textarea or anything

              The header() command must come before anything is output to the browser, including text, spaces, or newlines before the opening "<?php" in addition to obvious things like echo/print statements.

                so what, have

                <?php header('Content-Type: text/plain'); ?>

                at the very top of index.php and then

                <?php include("./headline.txt"); ?> 

                where I want the output?

                  Yes, the header() function must be called before anything is outputted.

                  Instead of using include(), I would recommend instead using a function such as [man]readfile/man so that PHP doesn't evaluate the .txt file for PHP code.

                    Ok I've moved it to the very topmost part and still having no luck.

                    Warning: Cannot modify header information - headers already sent by (output started at /home/.wyla/calibretto/portslacrosse.com/index.php:3) in /home/.wyla/calibretto/portslacrosse.com/index.php on line 3

                    could something else be conflicting with it?

                      Doesn't look like you've moved it to the top of the script yet, as there's something on line 3 (or above perhaps) that is outputted data.

                        Moved it to line 1 and its annihilated all my html. essentially now it's displaying all my source code

                        take a look: http://www.portslacrosse.com/

                        Down where it says: Test line 1 etc is where I want the output.

                          You can not use that method if you want it to be part of an HTML document. Instead just output it between a pair of <pre> tags:

                          <html>
                          ...yadda yadda yadda...
                          <pre><?php readfile('text.txt'); ?></pre>
                          ...yadda yadda yadda...
                          </html>
                          
                            Write a Reply...