Update shipping price after picked price (PHP)

tharnpfeffa

New Member
I am working on a shopping cart for my site. In the cart area I pull shipping details from the db, including the price depending on zip code. When the user clicks "update cart".I am trying to update the total with the shipping price. If anyone can point me in the right direction of figuring this out.This is my function to display the cart.\[code\]function showCart() {$cart = $_SESSION['cart'];$zip = $_SESSION['zipcode'];if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM products WHERE id = '.$id; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="http://stackoverflow.com/questions/10559690/cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' by '.$author.'</td>'; $output[] = '<td>$'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>$'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $sql = 'SELECT * FROM zipcodes WHERE zipcode = '.$zip; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $output[] = '<label>Price: </label><select name="delivery" id="delivery" class="long-field">'; $output[] = '<option value="">Please Select</option>'; $output[] = '<option value='http://stackoverflow.com/questions/10559690/.$row['free_dev'].'>$'.$row['free_dev'].'</option>'; $output[] = '<option value='http://stackoverflow.com/questions/10559690/.$row['next_day_dev'].'>$'.$row['next_day_dev'].'</option>'; $output[] = '</select>'; } $output[] = '<p>Delivery Charge: <strong>$'.$delivery.'</strong></p>'; $output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>';} else { $output[] = '<p>You shopping cart is empty.</p>';}return join('',$output);\[/code\]}And this the update part via cart.php\[code\]case 'update':if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } }}$cart = $newcart;break;\[/code\]Thank you. Any help is greatly appreciated!PS i am using the tutorial for the cart from here http://v3.thewatchmakerproject.com/journal/276/
 
Back
Top