bionic25 Posted September 2, 2009 Share Posted September 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/ Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 when you create a cookie, i believe the cookie's name has to be enclosed in quotes setcookie("pid", $totalPid, time()+3600*24); Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/#findComment-911166 Share on other sites More sharing options...
bionic25 Posted September 2, 2009 Author Share Posted September 2, 2009 really? because it sets the cookie fine in the bit to add the cookie its only the code to delete it that doesnt work? and i dont use quotes in the cookie bit that works Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/#findComment-911169 Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/#findComment-911172 Share on other sites More sharing options...
bionic25 Posted September 2, 2009 Author Share Posted September 2, 2009 thanks for the answer. tried adding the quotes but unfortunately no luck. any other ideas anyone\?? Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/#findComment-911176 Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 did you try doing a print_r on the cookie array? Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/#findComment-911180 Share on other sites More sharing options...
bionic25 Posted September 3, 2009 Author Share Posted September 3, 2009 I feel like the biggest n00b ever. i only thought it wasnt working cos it wasnt displaying anything and id forgotten to put the header redirect at the bottom. blank page + me being an idiot = me thinking php hasnt executed due to error Quote Link to comment https://forums.phpfreaks.com/topic/172883-substr_replace-problem/#findComment-911579 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.