How to save textarea content inside a .txt file wi

2019-08-21 16:23发布

问题:

i'm a beginner with php, and i'm trying to save textarea content inside a news.txt file.

After saving, i'll display the results in another page. Everything works fine, the ddate and the title is displayed perfectly, and even the textarea content, but if i press enter to create a new line, it doesn't work anymore.

here's what happen and some code.

Notice: Undefined offset: 1 in C:\Virtual Servers\xampp\htdocs\read.php on line 17

Notice: Undefined offset: 2 in C:\Virtual Servers\xampp\htdocs\read.php on line 18

and my code:

read.php page

<html>
    <head>
    <style>
    .news{background:#f1f1f1;width:960px;height:500px;margin-bottom:15px;}
    .title{color:red;}
    .date{font-style:italic;}
    .text{color:green;font-weight:bold;}
    </style>
    </head>
    <body>
    <?php
        $i = 0;
        $array = file("news/news.txt");
        foreach($array as $row){
            $split = explode('|', "$row");
            $data = $split[0];
            $titolo = $split[1];
            $testo = $split[2];
            $i++;
            print '<div id="'.$i.'" class="news"><div class="date">'.$data.'</div><div class="title">'.$titolo.'</div><div class="text">'.$testo.'</div></div>';
            }       
    ?>

    </body>
    </html>

index.php page:

<div class="container">
      <form name="myform" onsubmit="return validateForm()" action="welcome.php" method="post">
        <?php echo date('d-m-Y'); ?></br>
        <h1>Titolo:</h1><input type="text" id="Title" name="titolo" class="textStyleTitle"><br>
        <p>Testo:</p><textarea id="Text" name="testo" class="textStyleText"></textarea><br>
        <input type="submit" value="salva">
       </form>
    </div>

Any solutions?

thanks a lot.

!!UPDATE!! With the solution mentioned below, i've solved in part. I have no errors, but here's how it looks like:

    <body>
    <div id="1" class="news">
          <div class="date">04-01-2014</div>
          <div class="title">Testing title</div>
          <div class="text">Testing textarea row 1 (enter pressed)</div>
    </div>
    <div id="2" class="news">
          <div class="date">Testing textarea row 2 (enter pressed)</div>
          <div class="title"></div><div class="text"></div></div>
    <div id="3" class="news">
          <div class="date">Testing textarea row 3 (enter pressed)</div>
         <div class="title"></div><div class="text"></div></div>
   <div id="4" class="news">
          <div class="date">Testing textarea row 4</div>
          <div class="title"></div><div class="text"></div>
   </div></body>

unfortunally, what i'm tryin' to reach is something like this:

<body>
<div id="1" class="news">
     <div class="date">04-01-2014</div>
     <div class="title">Testing title</div>
     <div class="text">Testing textarea row 1 (enter pressed)
                       Testing textarea row 2 (enter pressed)
                       Testing textarea row 3 (enter pressed)
                       Testing textarea row 4
     </div>
</div>

回答1:

first remove quotes for $row in explode make as below: $split = explode('|', $row); then

use:

 $data = (isset($split[0])?$split[0]:"");

instead

 $data = $split[0];

same for $split[1] and $split[2]



回答2:

You have made a mistake here : $split = explode('|','$row'), No need to set $row into quotes, because it will use it as a string not as array

You need to use thhis $split = explode('|',$row) to explode you string into array



回答3:

try this code.

 foreach($array as $row){
            $data = $titolo = $testo = "";// initialize the valiables 
            $split = explode('|', $row);
            $data = (isset($split[0])?$split[0]:"");
            $titolo = (isset($split[1])?$split[1]:"");
            $testo = (isset($split[2])?$split[2]:"");
            $i++;
            print '<div id="'.$i.'" class="news"><div class="date">'.$data.'</div><div class="title">'.$titolo.'</div><div class="text">'.$testo.'</div></div>';
            }