Deleting in shopping cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Deleting in shopping cart

Post by kpraman »

Hello,

I want to delete the item in shopping cart. Its not working. Can anyone tell me, how to do?

Code: Select all

$cardId=$_SESSION['cartId']; 

$delCart=$_GET['delCart'];

//  Not working- start // 
if($delCart=="delCart")
  { 
       $delId=$_GET['delId'];
       unset($cardId[$hiddel]); 
  } 

// Not working - end// 


$uniqueCart=array_unique($cardId); 
$arrayCount=array_count_values($cardId); 

foreach($uniqueCart as $cardIds) 
  { 
      //code for fetch data from database using $cardIds. 
      //displaying the values. 
     <td align='center'><a href='cart.php?delCart=delCart&delId=$cardIds'>DELETE</a></td> //for sending value to delete. 

   }
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What's $hiddel?
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

it should be delId.

Still its not working
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Don't know what's wrong but try

Code: Select all

<?php
error_reporting(E_ALL); ini_set('display_errors', true);
session_start();

if ( !isset($_SESSION['cart']) || !is_array($_SESSION['cart']) || isset($_GET['init'])) {
	$_SESSION['cart'] = array();
	$_SESSION['cart'][1234] = array('name'=>'item 1234','quantity'=>'2');
	$_SESSION['cart'][5678] = array('name'=>'item 5678','quantity'=>'1');
	$_SESSION['cart'][9999] = array('name'=>'item 9999','quantity'=>'9');
}

if ( isset($_GET['delete']) ) {
	unset($_SESSION['cart'][$_GET['delete']]);
}
?>
<html>
	<head><title>cart test</title></head>
	<body>
		<table border="1">
			<tr><th colspan="2">name</th><th>quantity</th></tr>
<?php
foreach($_SESSION['cart'] as $id=>$item) { ?>
			<tr>
				<td><a href="?delete=<?php echo $id; ?>">DELETE</a></td>
				<td><?php echo $item['name']; ?></td>
				<td><?php echo $item['name']; ?></td>
			</tr>
<?php
}
?>
		</table>
		<a href="?init=1">new cart</a>
	</body>
</html>
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

Thanx volka, your code works fine. But, mine is not.

When ever i delete, the value i am passing is itemid's. unset unsets based on the index (i am not sure).

I am pasting the whole code.

Code: Select all

//this is in prod show page.

	if (!isset($_SESSION['cartId']) || !is_array($_SESSION['cartId'])) { 
			$_SESSION['cartId'] = array(); 
	} 
	$_SESSION['cartId'][] = $_POST['cartId'];
	$_SESSION['cartId'];
//

// cart page//

$cardId=$_SESSION['cartId'];

$Submit=trim($_POST['Submit']);

$delCart=$_GET['delCart'];

if($delCart=="delCart")
  {
    echo $delId=$_GET['delId'];
	unset($cardId[$delId]);
  }

$uniqueCart=array_unique($cardId);
$arrayCount=array_count_values($cardId);

$keys=array_keys($cardId);

//print_r($keys);

if($Submit=="Continue Shopping")
  {
     header("Location: prodshow.php");
  }


foreach($uniqueCart as $cardIds)
  {
    $query=mysql_query("SELECT * FROM prod WHERE prodId=$cardIds");
	if(!empty($query))
	 {
		$fetch=mysql_fetch_array($query);
		$expat=explode('@',$fetch['pictureName']);
		$expdot=explode('.',$expat[1]);
		$price=$arrayCount[$cardIds]*$fetch['picturePrice'];
		 $contents.="<tr>
		              <td align='center'><a href='cart.php?delCart=delCart&delId='$cardIds'>DELETE</a></td>
					  <td align='center'>$cardIds</td>
					  <td colspan='2' align='center'>$expdot[0] ($arrayCount[$cardIds])</td><td align='center'>$fetch[picturePrice]</td>
					  <td align='right'>$price</td>
					</tr>";
				$sub_total=$arrayCount[$cardIds] * $fetch['picturePrice'];
				$total=$total+$sub_total;
	 }
   $tmp=ReadTemplate("html/cart.html");
  }
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you have an array

Code: Select all

$arr = array(2=>'a', 9=>'b', 'x'=>'y');
you can use

Code: Select all

unset($arr[2])
or

Code: Select all

unset($arr['x']);
but not

Code: Select all

unset($arr['y']);
but that's exactly what you're doing.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Yes, that's what i pointed out, in my last reply.

for adding to the cart i am using,

[syntax="html"]<input type='hidden' name='cartId' value='$r[prodId]'><input type='submit' name='dowCart' value='Add to cart'>
for downCart[/syntax]

Code: Select all

if(!empty($dowCart))
 {
	if (!isset($_SESSION['cartId']) || !is_array($_SESSION['cartId'])) { 
			$_SESSION['cartId'] = array(); 
	} 
	$_SESSION['cartId'][] = $_POST['cartId'];
	$_SESSION['cartId'];
	
	print_r($_SESSION['cartId']);
	
}
i am getting the array like, Array ( [0] => 3 [1] => 2 [2] => 1 )

so, how to pass the values as index?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Deleting in shopping cart

Post by Christopher »

kpraman wrote:I want to delete the item in shopping cart. Its not working. Can anyone tell me, how to do?
In your example you are making a copy of the session variable containing the cart and then deleting from it. That will not change the array in the session. You need to either assign back or operate directly on the session var.
(#10850)
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

I wrote like this,

Code: Select all

if($delCart=="delCart")
		  {
						
			$delId=$_GET['delId'];
			$ar=array_search($delId,$cardId);
			$cardId=array_unique($cardId);
			unset($cardId[$ar]);
			$_SESSION['cartId']=$cardId; //reassigning 
                                }
This seems to be working. As you can see i am searching the index with the prodId and then unsetting.

I want to pass the prodIds to the session and then delete their ids.
Post Reply