0

I have following situation, I get several checkboxes, each of checkboxes has a value like 1, 2, 3 etc, this reffares to a certain position in array and certain line in a text file.

I need to somehow figure out how to update this certain line in a text file for if related checkbox was selected. each input has a name of visitProperty[]

foreach($_POST['visitProperty'] as $check) {
        $fileName = "pdata.txt";
        $fileContent = file($fileName);
        $readFile = fopen($fileName, "w+");
        $fileContent[$check] = "TEST TO SEE IF LINE REPLACED";
        $update = fwrite($readFile, $fileContent[$check]);
        fclose($readFile);
    }

This is what I tried, but it is not working.

Ilja
  • 44,142
  • 92
  • 275
  • 498
  • @AmalMurali yea, I've read that, but am still confused to how it works. – Ilja Feb 25 '14 at 17:30
  • If your question is "how the solution implemented in the other answer works", then perhaps you should ask that instead. – Amal Murali Feb 25 '14 at 17:31
  • if you want to know how it works,you should read documentation on each function – ɹɐqʞɐ zoɹǝɟ Feb 25 '14 at 17:35
  • @AmalMurali sure, but that question is looking up a certain phrase / word to replace if I am correct, whether as I'm looking to replace a specific line, knowing what line number needs to be replaced. – Ilja Feb 25 '14 at 17:36
  • Then why not just read file into array, replaces lines in the array, then place the entire array back into the file with file_put_contents (see http://php.net/file_put_contents). – user2537383 Feb 25 '14 at 17:41
  • I made an answer below of what I think you are looking for...is `$check` the index of the array you are trying to replace? – user2537383 Feb 25 '14 at 17:45

1 Answers1

1

Is this what you are looking for? Where $check is the index in the array?

$fileName = "pdata.txt";
$fileContent = file($fileName);
foreach($_POST['visitProperty'] as $check) {
    $fileContent[$check] = "TEST TO SEE IF LINE REPLACED";
}
file_put_contents ($fileName, implode("\n", $fileContent));
user2537383
  • 315
  • 8
  • 19