Please Help Modification of this code should not repeat n1 When entering data into a text file Do not repeat text when you add a new message with
<?php
if(isset($_POST['send'])){
$n1 = trim($_POST['n1']);
$n2 = trim($_POST['n2']);
$fileLocation = getenv("DOCUMENT_ROOT") . "/te.txt";
$file = fopen($fileLocation,"a");
$n1 = $_POST['n1'];
$n2 = $_POST['n2'];
fwrite($file,$n1.'|'.$n2."
");
fclose($file);
}
?>
</div>
Try this,
<?php
if(isset($_POST['send'])){
$n1 = trim($_POST['n1']);
$n2 = trim($_POST['n2']);
$fileLocation = getenv("DOCUMENT_ROOT") . "/te.txt";
$file = fopen($fileLocation,"r+");
$content = fread($file, filesize($fileLocation));
echo $content;
if(strstr($content, "|")){
fwrite($file,$n2."
");
}else{
fwrite($file,$n1.'|'.$n2."
");
}
fclose($file);
}
?>