DevLiv
DevLiv

Reputation: 573

Increment value in one text file and write text to another

I have a HTML script of which contains a form, this form submits a Name value to a PHP script. In this PHP script I open two different text files, first file is to get the number inside and then increment it by 1. The other file is to open and then write the newly incremented number along with the Name value from the Post. The first file of which only has a number inside starts on "0" and this is where I am having issues. When running the code, nothing happens, the form is submitted perfectly and the PHP script is called. But the only value inside the two different text files is "0" in both of them. Instead it should have "1" in the "amount.txt" file and "Text to appear: 1 Other text: Name" in the "textContent.txt" file.

I am not completely sure where I am wrong, to me it seems theoretically correct.

Underneath here is the PHP part, which is the part that isn't working.

$nam = $_POST['Name'];

$pastAmount = (int)file_get_contents('/user/site/amount.txt');
$fileOpen1 = '/user/site/amount.txt';
$newAmount = $pastAmount++;
file_put_contents($fileOpen1, $newAmount);

$fileOpen2 = '/user/site/textContent.txt';

$fileWrite2 = fopen($fileOpen2 , 'a');
$ordTxt = 'Text to appear:  ' + $newAmount + 'Other text: ' + $nam;
fwrite($fileWrite2, $ordTxt . PHP_EOL);
fclose($fileWrite2);

Upvotes: 1

Views: 826

Answers (2)

Guillaume
Guillaume

Reputation: 586

Instead of:

$newAmount = $pastAmount++;

You should use:

$newAmount = $pastAmount + 1;

Because $pastAmount++ will change the value of $pastAmount directly.

Then instead of

$ordTxt = 'Text to appear:  ' + $newAmount + 'Other text: ' + $nam;

You should use :

$ordTxt = 'Text to appear:  '.$newAmount.' Other text: '.$nam;

Because in PHP we are using the . for the concatenation.

PHP Code :

<?php
$nam = $_POST['Name'];


// Read the value in the file amount
$filename = "./amount.txt";
$file = fopen($filename, "r");
$pastAmount = fread($file, filesize($filename));
$newAmount = $pastAmount + 1;
echo "Past amount: ".$pastAmount."-----New amount:".$newAmount;
fclose($file);

// Write the value in the file amount
$file = fopen($filename, "w+");
fwrite($file, $newAmount);
fclose($file);


// Write your second file 
$fileOpen2 = './textContent.txt';
$fileWrite2 = fopen($fileOpen2 , 'w+  ');
$ordTxt = 'Text to appear:  '.$newAmount.' Other text: '.$nam;
fwrite($fileWrite2, $ordTxt . PHP_EOL);
fclose($fileWrite2);
?>

Upvotes: 1

Junaid
Junaid

Reputation: 1300

First of all, errors in your code:

  1. $newAmount = $pastAmount++; => This will assign $pastAmount's value and then increment the value which is not what you're aiming for.
  2. $ordTxt = 'Text to appear: ' + $newAmount + 'Other text: ' + $nam; => Concatenation in PHP is done with a . and not a +

Correct code:

$nam = $_POST['Name'];

$pastAmount = (int)file_get_contents('/user/site/amount.txt');
$fileOpen1 = '/user/site/amount.txt';
$newAmount = $pastAmount + 1;
// or
// $newAmount = ++$pastAmount;

file_put_contents($fileOpen1, $newAmount);

$fileOpen2 = '/user/site/textContent.txt';

$fileWrite2 = fopen($fileOpen2 , 'a');
$ordTxt = 'Text to appear:  ' . $newAmount . 'Other text: ' . $nam;
fwrite($fileWrite2, $ordTxt . PHP_EOL);
fclose($fileWrite2);

Upvotes: 1

Related Questions