Page 1 of 1
Deleting in shopping cart
Posted: Tue Dec 12, 2006 5:54 am
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.
}
Posted: Tue Dec 12, 2006 6:18 am
by volka
What's $hiddel?
Posted: Tue Dec 12, 2006 6:28 am
by kpraman
it should be delId.
Still its not working
Posted: Tue Dec 12, 2006 6:44 am
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>
Posted: Tue Dec 12, 2006 7:09 am
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");
}
Posted: Tue Dec 12, 2006 9:57 am
by volka
If you have an array
Code: Select all
$arr = array(2=>'a', 9=>'b', 'x'=>'y');
you can use
or
but not
but that's exactly what you're doing.
Posted: Tue Dec 12, 2006 11:25 pm
by kpraman
feyd | Please use 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
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]
Re: Deleting in shopping cart
Posted: Wed Dec 13, 2006 12:41 am
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.
Posted: Wed Dec 13, 2006 1:15 am
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.