Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Help with arrays and sessions

$
0
0

I've been working on the delete item from cart functionality all day. Still cannot figure this out.

 

When I add 2+ items to my cart, and try to delete any item but the last item added, the script deletes the listing the last item added no matter which one I'm attempting to delete. I could do this in C++ but the php solution is evading me. This has frustrated me today for over 4 hours just this one little problem. Can somebody please shine light on this for me, to help me see why I'm so close to finishing the delete functionality but it won't quite work. Here's my code:

 

header.html (included in cart at top):

session_start();

// For personal debugging -- REMOVE WHEN DONE //
if ($_GET['des']) {
	session_destroy();
	unset($_GET['des']);
}

// ---- //

// Count Cart Items //
if (empty($_SESSION['count_cart'])) {
	$_SESSION['count_cart'] = 0;
}

// Handle Delete Item //
if ($_POST['submitted']) {
	$_SESSION['count_cart']--;
	$delItem = $_POST['delItem'];
	unset($_SESSION['item'][$delItem]);
	unset($_POST['submitted']);
}

// Handle setting sessions //
if ($_POST['name'] == $PHPSESSID && isset($_POST['mdl_key'])) {
	$idx = $_SESSION['count_cart'];
	$_SESSION['count_cart']++;
	$_SESSION['item'][$idx][0] = $_POST['mdl_key'];
	$_SESSION['item'][$idx][1] = $_POST['model'];
	$_SESSION['item'][$idx][2] = $_POST['manufacturer'];
	$_SESSION['item'][$idx][3] = $_POST['sellprice'];
	$_SESSION['uri'][$idx] = $_POST['uri'];
}

And here is the code for the shoppingcart_customer.php page where the above is included with include:

<form name="del" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$i=0;
$count = $i + 1;
foreach ($_SESSION['item'] as $key=>$v1) { 
	if (isset($_SESSION['item'][$key])) {
		echo "<tr><td width='30px'></td><td bgcolor='white'>".$count.".) ";
		foreach ($v1 as $v2){
	    	echo $v2." ";
	    }
	}
echo " - <a href='".$_SESSION['uri'][$i]."' target='_blank'>View Details</a> | 
<input type='submit' value='Delete' name='delete'/>
<input type='hidden' name='delItem' value='".$i."'/>
<input type='hidden' name='submitted' value='TRUE'/>
</td><td width='80px'></td></tr>";
$i++;
}
?>
</form>

 

and here is the html $_POST code:

<form enctype="multipart/form-data" action="shoppingcart-customer.php" method="post">
<input type="hidden" name="name" value="<?php echo $PHPSESSID; ?>"/>
<input type="hidden" name="mdl_key" value="<?php echo $mdl_key; ?>"/>
<input type="hidden" name="model" value="<?php echo $model; ?>"/>
<input type="hidden" name="manufacturer" value="<?php echo $manufacturer; ?>"/>
<input type="hidden" name="sellprice" value="<?php echo $sellprice; ?>"/>
<input type="hidden" name="uri" value="<?php echo $_SERVER['REQUEST_URI']; ?>"/>
<input type="submit" name="addtocart" value="Add To Cart" id="hyperlink-style-button"/>
	</form>

Viewing all articles
Browse latest Browse all 13200

Trending Articles