kate_30 Posted August 6, 2007 Share Posted August 6, 2007 Hi all I have a txt file with this values: parameter1 parameter two third value others values... From a form, I would edit the parameters above: <form method="POST" action="edit.php"> <?php $rows = file("file.txt"); for($i=0; $i < count($rows); $i++) { echo "<input type=text name=$rows[$i] value=$rows[$i]><br />"; } ?> <input type="submit" value="Edit" name="submit"></p> </form> <?php if (isset($_POST['submit'])) { $upd= $_POST[$rows[$i]]; if(!($fp = fopen("file.txt","r+"))) { echo "Error"; } else { fwrite($fp, $upd); fclose($fp); } } ?> I use this but doesn't works. Where it is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/63588-update-a-row-in-txt-file/ Share on other sites More sharing options...
GingerRobot Posted August 6, 2007 Share Posted August 6, 2007 Im guessing by "doesn't work" you mean that when you try and edit the file, it doesn't edit successfully. Try: <form method="POST" action="edit.php"> <?php $rows = file("file.txt"); for($i=0; $i < count($rows); $i++) { echo "<input type='text' name='lines[$i]' value='$rows[$i]'><br />"; } ?> <input type="submit" value="Edit" name="submit"></p> </form> <?php if (isset($_POST['submit'])) { $towrite = implode("\n",$_POST['lines']); if(!($fp = fopen("file.txt","r+"))) { echo "Error"; } else { fwrite($fp, $towrite); fclose($fp); } } ?> It's untested, but give it a go. Notice the way that i've named the text fields as an array. Without doing this, you would have to open the file again, and extract each field name - since the name of the fields was what was on each line of the text file. Quote Link to comment https://forums.phpfreaks.com/topic/63588-update-a-row-in-txt-file/#findComment-316860 Share on other sites More sharing options...
mpharo Posted August 6, 2007 Share Posted August 6, 2007 <form method="POST" action="edit.php"> <?php $rows = file("file.txt"); foreach ($rows as $rows_num => $rows) { echo "<input type=text name=$rows_num value=$rows><br />"; } ?> <input type="submit" value="Edit" name="submit"></p> </form> <?php if (isset($_POST['submit'])) { $upd= $_POST[$rows]; if(!($fp = fopen("file.txt","r+"))) { echo "Error"; } else { fwrite($fp, $upd); fclose($fp); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63588-update-a-row-in-txt-file/#findComment-316863 Share on other sites More sharing options...
kate_30 Posted August 6, 2007 Author Share Posted August 6, 2007 Im guessing by "doesn't work" you mean that when you try and edit the file, it doesn't edit successfully. Try: <form method="POST" action="edit.php"> <?php $rows = file("file.txt"); for($i=0; $i < count($rows); $i++) { echo "<input type='text' name='lines[$i]' value='$rows[$i]'><br />"; } ?> <input type="submit" value="Edit" name="submit"></p> </form> <?php if (isset($_POST['submit'])) { $towrite = implode("\n",$_POST['lines']); if(!($fp = fopen("file.txt","r+"))) { echo "Error"; } else { fwrite($fp, $towrite); fclose($fp); } } ?> It's untested, but give it a go. Notice the way that i've named the text fields as an array. Without doing this, you would have to open the file again, and extract each field name - since the name of the fields was what was on each line of the text file. This works but a strange thing happens: try to edit, from input text, the second value "parameter two" to a thing as "parameter two onetwothreefourfive" Then click submit and after edit the value above to original "parameter two". Will be created another row in txt file!! Why? Quote Link to comment https://forums.phpfreaks.com/topic/63588-update-a-row-in-txt-file/#findComment-316892 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.