phpnewbie112 Posted October 10, 2008 Share Posted October 10, 2008 Hello, I am using fopen (...)to create and write to a new file .txt and fwrite(..) to write new values based on GET values. I would like to know how can I do a verification for the value so if it exists to overwrite otherwise add new. I am using to write: fwrite($fp,$MmCode."|".$Description); I wont to overwrite the whole line based on MmCode value for example: 165|Desc1 167|Desc2 138|Desc5 If we have 165|Desc12 again it shall replace 165|Desc12 your help is very much appreciated. thanks Link to comment https://forums.phpfreaks.com/topic/127836-overwrite-a-value-under-a-file/ Share on other sites More sharing options...
R0bb0b Posted October 10, 2008 Share Posted October 10, 2008 I would do it like this: <?php $rows = file("textfile.txt"); //read file into an array $data = array(); foreach($rows as $value) { list($key, $desc) explode("|", $value); $data[$key] = $desc; } if(count($_POST) > 0) { if(!empty($_POST['165'])) $data['165'] = $_POST['165']: if(!empty($_POST['167'])) $data['167'] = $_POST['167']: if(!empty($_POST['138'])) $data['138'] = $_POST['138']: // and so on $textdata = ""; foreach($data as $key=>$value) { $textdata .= $key . "|" . $value . "\n"; } $textdata = substr($textdata, 0, -2); //take off the last \n to eliminate confusion $fp = fopen('textfile.txt', 'w'); fwrite($fp, $textdata); //overwrite the existing data with the updated data fclose($fp); } ?> Link to comment https://forums.phpfreaks.com/topic/127836-overwrite-a-value-under-a-file/#findComment-661838 Share on other sites More sharing options...
phpnewbie112 Posted October 10, 2008 Author Share Posted October 10, 2008 thank you I will try it and get back to you Link to comment https://forums.phpfreaks.com/topic/127836-overwrite-a-value-under-a-file/#findComment-661974 Share on other sites More sharing options...
phpnewbie112 Posted October 10, 2008 Author Share Posted October 10, 2008 didn't work still storing same values twice Link to comment https://forums.phpfreaks.com/topic/127836-overwrite-a-value-under-a-file/#findComment-662015 Share on other sites More sharing options...
R0bb0b Posted October 10, 2008 Share Posted October 10, 2008 actually, when I tested it, I did find a couple of errors. But this works: <?php $rows = file("textfile.txt"); //read file into an array $data = array(); foreach($rows as $value) { list($key, $desc) = explode("|", $value); $data[$key] = $desc; } if(count($_POST) > 0) { if(!empty($_POST['165'])) $data['165'] = $_POST['165']; if(!empty($_POST['167'])) $data['167'] = $_POST['167']; if(!empty($_POST['138'])) $data['138'] = $_POST['138']; // and so on $textdata = ""; foreach($data as $key=>$value) { $textdata .= $key . "|" . $value . "\n"; } $textdata = substr($textdata, 0, -2); //take off the last \n to eliminate confusion $fp = fopen('textfile.txt', 'w'); fwrite($fp, $textdata); //overwrite the existing data with the updated data fclose($fp); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"> <table cellpadding="0" cellspacing="0"> <tr> <td>165</td> <td><input type="text" name="165" /></td> </tr> <tr> <td>167</td> <td><input type="text" name="167" /></td> </tr> <tr> <td>138</td> <td><input type="text" name="138" /></td> </tr> <tr> <td colspan="2"><input type="submit" name="btnSubmit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/127836-overwrite-a-value-under-a-file/#findComment-662205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.