Jump to content

I'm in a problem Save to text file without duplication


k7l2010

Recommended Posts

Please help and amend the code Save without repetition required $read[0] I hope that my example is clear to you I apologize for bothering you. I am not good in English

<?php

  $fileLocation = getenv("DOCUMENT_ROOT") . "/rt.txt";
  $file = fopen($fileLocation,"a");
$n1 = $_POST['n1'];
$n2 = $_POST['n2'];
 	for($i = 0; $i <  count($file); $i++){

 $read = explode("|", $file[$i]);
			if($n1 == $read[0]){
echo 'Repetition';
    }
    else
   {

  fwrite($file,$n1.'|'.$n2."\r\n");

}

 

Link to comment
Share on other sites

fopen() gives you a "resource" for you to read from and write to the file. "a" means to open the file in append mode, putting you at the end of the file and only allowing you to write.

If you want to read and write then use "a+" mode and fseek to the beginning. You can read each line with fgets to look for repetition.

You need to use a variable to remember whether you have found the repetition. Start the variable as false, then if you find the repetition set it to true. After the loop, if you did not find the repetition then you can fwrite() the new line (which PHP will put at the end of the file).

Remember to fclose at the end.

Link to comment
Share on other sites

Quote

Hello Please amend the code Thank you for your interaction😩

We will not write the code for you. Try to understand what I wrote in my reply and amend your code. If you have more problems when you are done then we can help you fix it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.