title: PHP code to edit different lines of a text file
body: <p>I have a text file in the following format, let's call it stats.txt:</p>
  
  <pre><code>valueA1, valueA2, valueA3, valueA4
  valueB1, valueB2, valueB3, valueB4
  valueC1, valueC2, valueC3, valueC4
  valueD1, valueD2, valueD3, valueD4
  </code></pre>
  
  <p>I want four text boxes or textareas to populate with this code in an html document and when form is submitted, append to the corresponding lines of the text file. I'm not very knowledgeable with php so can't figure out a way to do this.</p>
  
  <p>My current html file looks like this:</p>
  
  <pre><code>$url = 'editor.php';
  $file = 'stats.txt';
  
  // check if form has been submitted
  if (isset($_POST['text1']))
  {
      // save the text contents
      file_put_contents($file, $_POST['text1']);
  
      // redirect to form again
      header(sprintf('Location: %s', $url));
      printf('&lt;a href="%s"&gt;Updated&lt;/a&gt;.', htmlspecialchars($url));
      exit();
  }
  
  // read the textfile
  $text = file_get_contents($file);
  
  ?&gt;
  </code></pre>
  
  <p>I couldn't figure out how to loop between the lines of the text file, so have this chunk of horrible code to get the value of each line:</p>
  
  <pre><code>    &lt;?php
      //for Line 1
      $myLine1 = 1 ; 
      $linefile1 = new SplFileObject($file);
      $linefile1-&gt;seek($myLine1-1);
  
      //for Line 2
      $myLine2 = 2 ; 
      $linefile2 = new SplFileObject($file);
      $linefile2-&gt;seek($myLine2-1);
  
      //for Line 3
      $myLine3 = 3 ; 
      $linefile3 = new SplFileObject($file);
      $linefile3-&gt;seek($myLine3-1);
  
      //for Line 4
      $myLine4 = 4 ; 
      $linefile4 = new SplFileObject($file);
      $linefile4-&gt;seek($myLine4-1);
      ?&gt;
  </code></pre>
  
  <p>And the form looks like this:</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
  #stuff 1
  &lt;textarea id="stuff" name="stuff[]"&gt;&lt;?php echo htmlspecialchars($linefile1) ?&gt;&lt;/textarea&gt;
  #stuff 2
  &lt;textarea id="stuff" name="stuff[]"&gt;&lt;?php echo htmlspecialchars($linefile2) ?&gt;&lt;/textarea&gt;
  #stuff 3
  &lt;textarea id="stuff" name="stuff[]"&gt;&lt;?php echo htmlspecialchars($linefile3) ?&gt;&lt;/textarea&gt;
  #stuff 4
  &lt;textarea id="stuff" name="stuff[]"&gt;&lt;?php echo htmlspecialchars($linefile4) ?&gt;&lt;/textarea&gt;
  &lt;input type="submit" /&gt;
  &lt;input type="reset" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Can someone help me with this please?</p>