pFC Posted December 18, 2009 Share Posted December 18, 2009 I test to create a simple shopping site and getting headache to update a previous session. Here the codes. index.php <ul> <?php while($product = $db->fetch($qb)) { ?> <li style="float:left; width:200px; list-style:none;"> <strong><?php echo $product['title'];?></strong> <form action="update.php" method="post"> <input type="hidden" name="item-id[]" value="<?php echo $product['id'];?>" /> <input type="hidden" name="item-name[]" value="<?php echo $product['title'];?>" /> <select name="item-qty[]" onchange="this.form.submit()"> <?php for($i=0;$i<11;$i++) { ?> <option<?php if($_SESSION['order'][''.$product['id'].'']['item-qty'] == $i){ ?> selected="selected"<? }?> value="<?php echo $i;?>"><?php echo $i; ?></option> <?php } ?> </select> <?php if($_SESSION['order'][$product['id']]['item-price'] == '') { echo '$ 0'; unset($_SESSION['order'][$product['id']]); } else { echo '$ '.$_SESSION['order'][$product['id']]['item-price']; }?> <?php if (sizeof($_SESSION['order']) < 1) { unset($_SESSION['order'],$_SESSION['count']); } ?> </form> </li> <?php $j++; } ?> </ul> update.php <?php $qty = $_POST['item-qty']; $id = $_POST['item-id']; $i = count($q); $n = 0; while ($n < $i) { $select = 'SELECT * FROM price'; $query = $db->rq($select); $price = $db->fetch($query); $get = $price['normal_price']; $price = $get * $_POST['item-qty'][$n]; $_SESSION['order'][$_POST['item-id'][$n]] = array("item-id" => $_POST['item-id'][$n], "item-qty" => $_POST['item-qty'][$n], "item-name" => $_POST['item-name'][$n], "item-price" => $price, ); $qty_sum = 0; foreach ($_SESSION['order'] as $key => $value) { $qty_sum += $value['item-qty']; $_SESSION['count'] = $qty_sum; } if ($qty_sum >= 3) { $select = 'SELECT * FROM price'; $query = $db->rq($select); $price = $db->fetch($query); $get = $price['member_price']; $price = $get * $_POST['item-qty'][$n]; $_SESSION['order'][$_POST['item-id'][$n]] = array("item-id" => $_POST['item-id'][$n], "item-qty" => $_POST['item-qty'][$n], "item-name" => $_POST['item-name'][$n], "item-price" => $price, ); } $n++; header('location:index.php'); exit(); } ?> 1. Here have a different price. Normal Price and Member Price 2. If total of quantity <= 2 price per quantity should be $200 - Normal Price 3. If total of quantity >= 3 price per quantity should be $150 - Member Price Question 1. How to update current session price to be $150/per qty if buyer select total of quantity more than 3 and back to normal price if qty below than 3. 2. Now it only working on last item. How to fix it? More clearly take a look my demo page here Link to comment https://forums.phpfreaks.com/topic/185628-update-session/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.