need help on shoping cart check out!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

need help on shoping cart check out!

Post by lukelee »

Hi, guys, I am making a simple shopping cart, now i got problem on check out function. i want to save the product information into database after click "checkout".
because people may have different products in their shopping cart, so i made an array to display the items. but when I POST those items to checkout_process.php, it only posts the latest item's information.
can anyone help? thanks, here is a part of my code:

Code: Select all

 
function checkout() {
      $output[] = '<form action="checkout_process.php" method="post" id="cart">';
   global $db;
   $cart = $_SESSION['cart'];
   if ($cart) {
      $items = explode(',',$cart);
      $contents = array();
      foreach ($items as $item) {
         $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
      }
      $output[] = '<table>';
      foreach ($contents as $id=>$qty) {
         $sql = 'SELECT * FROM product WHERE product_id = '.$id;
         $result = $db->query($sql);
         $row = $result->fetch();
         extract($row);
         $output[] = '<tr><td><input type="hidden" name="model" value="'.$model.'"/></td>';
         $output[] = '<td><input type="hidden" name="quantity'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
         $output[] = '<td><input type="hidden" name="price" value="'.$price.'"/></td>';
         $output[] = '<td><input type="hidden" name="price" value="'.($price * $qty).'"/></td>';
         $total += $price * $qty;
         $output[] = '</tr>';
      }
            
      $output[] = '</table>';
      $output[] = '<div><button type="submit">check out</button></div>';
      $output[] = '</form>';
   } else {
      $output[] = '<p>You shopping cart is empty.</p>';
   }
   return join('',$output);
}
 
 
Post Reply