Jump to content

Recommended Posts

hey people, need some help with this ecommerce site im working on.

 

this is the code to set a cookie which contains the 'basket' or 'cart' info for the user's session when they click to add a product. it sets the cookie to be a 5 digit string with the product's 3 digit id number then a comma then a space. eg. '123, '.

 

function addItem()
{
if (isset($_COOKIE['pid']))
{
	$existPid = $_COOKIE['pid'];
	$newPid = $_GET['pid'];
	$sep = ", ";
	$totalPid = $existPid.$sep.$newPid;
	$display = $totalPid;

	//set the cookie
	setcookie(pid, $totalPid, time()+3600*24);
}
else
{
	$newPid = $_GET['pid'];
	setcookie(pid, $newPid, time()+3600*24);
}
}

//add item function called
addItem();

 

the problem is the code i'm using to delete an individual item from the cart isnt working.

 

here it is

		$item = $_GET['delItem'];

	if (!isset($_GET['delItem']))
	{	
	setcookie("pid","", time()-3600);
	}

	else if($_GET['delItem'] == 1)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',0,5);
		setcookie(pid, $newPid, time()+3600*24);
	}		
	else if($_GET['delItem'] == 2)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',5,10);
		setcookie(pid, $newPid, time()+3600*24);
	}		
	else if($_GET['delItem'] == 3)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',10,15);
		setcookie(pid, $newPid, time()+3600*24);
	}		
	else if($_GET['delItem'] == 4)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',15,20);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 5)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',20,25);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 6)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',25,30);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 7)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',30,35);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',35,40);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 9)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',40,45);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 10)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',45,50);
		setcookie(pid, $newPid, time()+3600*24);
	}
	else if($_GET['delItem'] == 11)
	{
		$str = $_COOKIE['pid'];
		$newPid = substr_replace($str,'',50,55);
		setcookie(pid, $newPid, time()+3600*24);
	}

 

i made this code from scratch so doubt its done properly as im new to php. however if some could tell me why the $newPid variable is not changing the pid cookie to the correct string with the right bit removed?

 

 

thanks in advance for any help

Link to comment
https://forums.phpfreaks.com/topic/172883-substr_replace-problem/
Share on other sites

Oh ok, well I don't really use cookies ever, but I've always seen them done that way.

 

Upon looking at the PHP.net page for cookies, yes you do need to surround them with quotes. Perhaps you cookie creation page isn't actually creating any cookies, or creating them in a way you don't expect, and thats what is causing them to not delete.

 

http://us.php.net/setcookie

 

try echoing all your cookies via

print_r($_COOKIE);

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.