raptor30506090 Posted June 14, 2012 Share Posted June 14, 2012 Hi guy could any the point me in the right direction: Im trying to build my first shopping cart but stuck at the start i now im going to use sessions this is what i have. if (!isset($_session['cart'])) $_SESSION["cart"]= '$id' ; if (isset($_POST['id']) &&!empty($_POST['id']) && isset($_POST['cost']) &&!empty($_POST['cost'])) { $id = $_POST['id']; $cost = $_POST['cost']; $item = array($id => $cost); $_SESSION['cart']=$item; foreach($_SESSION['cart'] as $key => $cost){ echo $key . ' '. $cost; } } echo '<pre>'; print_r($_SESSION); echo '</pre>'; the question is i have the id and the cost from the form but in the cart array how do i add to the cost each time button pressed Many thanks Quote Link to comment Share on other sites More sharing options...
dawsba Posted June 14, 2012 Share Posted June 14, 2012 not 100% sure what you mean are you looking to have an add qty button to add multiple items, perhaps ajax? or a refresh each time they click add item: in which case i would structure your session array alittle different and add qty to the fields ie: if (!isset($_session['cart'])) $_SESSION["cart"]= '$id' ; if (isset($_POST['id']) &&!empty($_POST['id']) && isset($_POST['cost']) &&!empty($_POST['cost'])) { $id = $_POST['id']; $cost = $_POST['cost']; if($_SESSION['cart'][$id][qty]>0) { $_SESSION['cart'][$id][qty]++; } else { $_SESSION['cart'][$id][cost]=$cost); $_SESSION['cart'][$id][qty]=1; } foreach($_SESSION['cart'] as $key => $arr){ echo $key . ' '. $arr[cost].' '.$arr[qty].' TOTAL:$pound;'.number_format($arr[qty]*$arr[cost],2)); } } echo '<pre>'; print_r($_SESSION); echo '</pre>'; Quote Link to comment 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.