i have small page with cart. All things is storage in session. Now i need to save this data to mysql databse.
I think, that i need use foreach cycle to do this, but i cant construct it. Does anybody know the solution?
This is the function that displays cart.
function showCart() {
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[] = '<form action="cart.php?action=update" method="post" id="cart">';
$total=0;
$output[] = '<table>';
foreach ($contents as $id_menu=>$qty) {
$sql = 'SELECT * FROM menu WHERE id_menu = '.$id_menu;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id_menu='.$id_menu.'" class="r">Delete</a></td>';
$output[] = '<td>' .$name. '</td>';
$output[] = '<td>' .$price.' Kč</td>';
$output[] = '<td><input type="text" name="qty'.$id_menu.'" value="'.$qty.'" size="5" maxlength="5" /></td>';
$output[] = '<td>' .($price * $qty).' Kč</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Total: <strong>'.$total.' EUR</strong></p>';
$output[] = '<div><button type="submit">Update</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>Cart is empty.</p>';
}
return join('',$output);
}