k7l2010 Posted May 10, 2020 Share Posted May 10, 2020 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/310746-im-in-a-problem-save-to-text-file-without-duplication/ Share on other sites More sharing options...
requinix Posted May 10, 2020 Share Posted May 10, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310746-im-in-a-problem-save-to-text-file-without-duplication/#findComment-1577827 Share on other sites More sharing options...
requinix Posted May 11, 2020 Share Posted May 11, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310746-im-in-a-problem-save-to-text-file-without-duplication/#findComment-1577829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.