0

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?

3
  • What is the question? Commented Nov 20, 2013 at 2:34
  • I want to change the last variable($qty) in each line if there is pricingID(first variable in the line) already occurs. Otherwise insert new line to text file Commented Nov 20, 2013 at 2:37
  • Sorry. Didn't see it. Well main thing that jumps out is you only open the file in write mode. Not gonna update it that way. Commented Nov 20, 2013 at 2:55

1 Answer 1

2

First of all, I'd recommend you not write your own flat file for the shopping cart, but rather create a shopping cart table in your database.

However, if you wish to proceed in this manner, what you want can be accomplished with a small change to your existing code.

if (file_exists($file))
{
    $count=count(file($file));
    $new_file_contents = ""; //contents to overwrite this file with

    while (!feof($file_handle) )
    {
        $line_of_text = fgets($file_handle);
        $parts = explode('|', $line_of_text);
        $pid=trim($parts[0]);
        if($pid==$pricingID)
        {
            $parts[2] = $qty + 1; //store incremented qty in array
            $line_of_text = implode("|", $parts); //use implode to put array back together
        }
        $new_file_contents .= $line_of_text; //store this in new file contents
    }
    fclose($file_handle);
}

file_put_contents($file, $new_file_contents);

However, I'd probably approach this with a regular expression. I feel it's an easier way to tackle this and doesn't deal with iterating over each line of the file.

if (file_exists($file))
{
   $file_contents = file_get_contents($file);

   $pattern = "/(${pid}\|[^\|]*\|)([0-9]+)/"; //establish regex pattern matching lines based on pid
   preg_match($pattern, $file_contents, $matches); //match the line of this pid
   $qty = $matches[2] + 1; //get the quantity currently in cart for this product and increment it
   $replace = '${1}' . $qty; //build our replacement string using a callback and new quantity
   $file_contents = preg_replace($pattern, $replace, $file_contents); //replace it in the file
   file_put_contents($file, $file_contents); //overwrite old file with updated quantity
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, let me try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.