I am creating text file to store my variables from one webpage. When a user clicks an add button(like shopping cart) details are entered into the corresponding text file in the server. My code is given below:
$file = "../file/".$profile.".txt";
$qty=1;
$file_handle = fopen($file, "rb");
$profileID=mysql_query("SELECT * FROM `profile` WHERE `name`='$profile'",$con);
$profileID = mysql_fetch_row($profileID);
$profileID=$profileID[0];
$current = file_get_contents($file);
//$current.=file_put_contents($file, $profile."\n");
$result="SELECT p.*, pk.*,p.id as PID FROM `pricing` p
JOIN (SELECT `distributor`,MAX(`version`) AS ver FROM `pricing` GROUP BY `distributor`) mx ON mx.ver = p.version AND p.distributor = mx.distributor
JOIN `product_picker` pk ON pk.code = p.code AND pk.profile_name=$profileID AND p.id=$productID";
$result=mysql_query($result);
while($row = mysql_fetch_array($result))
{
$pricingID=$row['PID'];
$code=$row['code'];
$buy=$row['buy'];
$markup=$row['custom markup'];
$sell=$buy*$markup;
$buy="$".number_format((float)$sell,2,'.','');
$stock=explode('.',$row['stock']);
$stock=$stock[0];
}
if (file_exists($file))
{
$count=count(file($file));
while (!feof($file_handle) )
{
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
$pid=trim($parts[0]);
if($pid==$pricingID)
{
$qty=$qty+1;
}
}
fclose($file_handle);
}
$current.=$pricingID."|".$code.",".$buy.",".$stock."|".$qty."\n";
file_put_contents($file, $current);
According to the code, the text file format will be:
793|EX-24-AFL,$2425.95,0|1
8078|EX-48-AFL,$3619.35,0|1
866|EX-PWR-320-AC,$303.24,20|1
793|EX-24-AFL,$2425.95,0|2
793|EX-24-AFL,$2425.95,0|3
The first column represents the id(793,8078,866). The above code inserted every time to the text file. But I need to change the last value(qty) if the text file's first column is id already exists.
So the output of text file should be
793|EX-24-AFL,$2425.95,0|3
8078|EX-48-AFL,$3619.35,0|1
866|EX-PWR-320-AC,$303.24,20|1
Anyone please help me?