Jack
Jack

Reputation: 753

Modifying a text file with php

I have two webpages a.php and b.php. The submitted values in texboxes will be written to a text file.

a.php :

 <html>

<?php

if (isset($_POST['Submit1'])) {

 $aa = $_POST['alpha'];

 $f = fopen("text.txt", "w");

 fwrite($f,$aa."\n");

 }
 else

 {
 $f= fopen("text.txt",'r');

  while ((!feof($f)) && ($found == 0)) {

  list($aa)=fscanf($f,"%f[^\n]");

   }

   }

  fclose($f);

   ?>
   <form action="a.php" name="Calculation" method="post">

   Alphabet: <INPUT TYPE ="TEXT" id="alph" Name "alpha"          VALUE="<?PHP print $aa; ?>">

   <Input Type = "Submit" Name = "Submit1" Value ="Save Parameters">

    </form>

    </html>

and b.php is:

<!DOCTYPE html>
<html>

<?php

if (isset($_POST['Submit1'])) {

$bb = $_POST['beta'];

 file_put_contents("text.txt", $bb."\n" , FILE_APPEND);

 }

 else

 {

 $f= fopen("text.txt",'r');

 while ((!feof($f)) && ($found == 0)) {

 list($aa)=fscanf($f,"%f[^\n]");
  list($bb)=fscanf($f,"%f");
 }
 }

 fclose($f);

  ?>
 <form action="b.php" name="Calculation" method="post">

 Alphabet: <INPUT TYPE ="TEXT" id="betaa" Name ="beta" size="5"     VALUE="<?PHP print $bb; ?>">

 <Input Type = "Submit" Name = "Submit1" Value ="Save Parameters">

  </form>

  </html>

The codes work fine and the values in the text boxes will be written to a textfile if I enter the first valu in a.php text box and the second value in b.php textbox. For example if I put aa in the textbox of a.php and bb in the text box of b.php I will get

aa bb

in my text file. However if I go back to my a.php file and put a new value in its text box I will loose bb. Is there a way to only change the value of the first line of the text box and keep the bb in second line of the text box? . The problem is that I will lose the values of my b.php script if I add a new value. I don't want to add to the number of lines in the text file. I was wondering if there is a way to rewrite on aa with cc for example.

Upvotes: 2

Views: 142

Answers (3)

Christian Vincenzo Traina
Christian Vincenzo Traina

Reputation: 10384

To change only some specific lines of your file you can use:

$file = "text.txt";
$aa = $_POST['alpha'];
$rows_of_file = file($file); // put your file in an array line by line
$rows_of_file[0] = $aa; // 0 indicate the first line of the file
file_put_contents($file, implode($rows_of_file)); //rewrite the content with the new changes

So you can change aa in cc, and bb remains unchanged

Upvotes: 1

splash58
splash58

Reputation: 26153

You can use json function to manipulate with vars independently:

$f = json_decode(file_get_contents("tt.txt"));
$f['aa']= $_POST['alpha'];
file_put_contents("tt.txt", json_encode($f));

and change to bb in 2nd file. To read just

$f = json_decode(file_get_contents("tt.txt"));
echo $f['aa'].' '.$f['bb'];

Upvotes: 1

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You're overwriting your file in a.php using the w switch.

Use the a switch to append.

$f = fopen("text.txt", "a");

Consult the manual:

Or, do as you did in the other file (b.php) using file_put_contents()

file_put_contents("text.txt", $bb."\n" , FILE_APPEND);
                                         ^^^^^^^^^^^

it does the same thing.


Edit:

"I have a webpage that has several text boxes that need to be filled and the values are saved in a text file when the submit button is hit. Then there is another webpage that has several text boxes as well that need to be filled out and the values will have to be added to the same text file.

So if the user after pressing the submit button decides to change a value of one text box in a.php and then hit submit again I will lose all the information in bb.php unless I add more lines to my text file with the new information."

An alternate method would be to use two seperate and different filenames while keeping what you have now.

Here is an example, and concatenating the files:

<?php 
$file1 = file_get_contents('file1.txt');
$file2 = file_get_contents('file2.txt');

$show_all = $file1 . $file2;

echo $show_all;
  • I used file_get_contents() in this example, since I do not know how you are presently showing the content in your webpage.

Sidenote:

If you want to add a line break between both, use <br>.

$show_all = $file1 . "<br>" . $file2;

or just a simple space:

$show_all = $file1 . " " . $file2;

Upvotes: 5

Related Questions