am trying to write a shopping cart script, that once they have confirmed their order it will insert into the database their order information.
Basically as a product can have a number of different quanitities that they wish to purchase, I want to create an associative array that has a product, linked to the quantity. I want to do this so that the order information stays in one row in the database, so in the 'product' row, it has values like (cow -> 2) cow being the product and 2 being the quantity.
Here is the code I am developing, and I have commented in the array which I am trying to create just the show my idea of thinking. any help, will be greatly appreciated!
PHP Code:
unction updateCart($product_id,$id){
if($_SESSION['cart']) {
foreach($_SESSION['cart'] as $product_id => $quantity) {
$total = 0;
$sql = sprintf("SELECT title, price, shipping FROM product WHERE pkID = %d;",$product_id);
$result = query($sql);
if(numRows($result) > 0 ){
$row = getResult($result);
$price = $row['price'];
$shipping = $row['ship'];
$line_cost = ($price + $shipping) * $quantity;
$total = $total + $line_cost;
$product[] = $row['title'] -> $quantity; //unsure of how to code
$sql2 = "INSERT INTO cart (userID, products, total, date) VALUES ('$id', '$product', '$total' '$date')";
$query = query($sql2);
}
}
}else{
$alert = 'No items in shopping cart.';
}
return $total;
}
|