Jump to content

overwrite a value under a file


phpnewbie112

Recommended Posts

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

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);
}
?>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.