Jump to content

Delete specific substring


ozzles2006

Recommended Posts

I'm trying to build an e-commerce type website...

I couldn't get a "shopping cart" array to store in a session variable, so I gave up and started on cookies instead. So.. I have a string that I'm getting from a cookie. It's in the format "1,2,1,3,4,5" and so on (no particular order) depending on what's been put into it.

I'm just wondering if theres any way to delete, for example, 1 instance of "2," or "5"? I can do it to remove all entries, but I only want it to do it once.
Link to comment
Share on other sites

Thought I should post the bit of code I'm dealing with.

[quote]$cart=$_COOKIE['cart'];
$item = $_POST['id'];
$qty = $_POST['qty'];

if (strlen($cart)==1) {

setcookie('cart','');
echo "<meta content='0;url=index.php?p=checkout' http-equiv='refresh' />";

} else {

$cart = explode("|", $_COOKIE['cart']);
$cart_count = array_count_values($cart);

foreach ($cart_count as $itemNum => $qty) {

}
echo "<meta content='0;url=index.php?p=checkout' http-equiv='refresh' />";
}[/quote]
Basically what I'm doing is using the array_count_values method to "group by" the item number.
So what I want to do is reduce the quantity for the item corresponding to the button that's been pressed and then regenerate the string and add it to the cookie again.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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