Inserer les donnees dans ma table commande
Salut
Je voudrais savoirs comment je peux inserrer mes donnees de panier dans la table commndes.
L'affiche de panier marche trés bien avec echo showCart(); je peux modifer ajouter supprimer mais tous ca reste dans la session. je voudrais apres que je valide la commane, cette derniere doit etre inserer dans ma base de donnees dans la table commande depuis les variables trouvants dans la seesion
voici mes codes.
page founctions.inc.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (empty($cart)) {
return '<span>Votre panier est vide</span>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<span>Vous avez '.count($items).' produit'.$s.' dans votre panier</span>';
}
}
function showCart() {
global $db , $total ;
$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">';
$output[] = '<table>';
$output[] = '<th>Description</th>';
$output[] = '<th>Prix</th>';
$output[] = '<th>Quantite </th>';
$output[] = '<th>Total</th>';
$output[] = '<th></th>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT id, titre, prix FROM films WHERE id = '.$id ;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$title = $row['titre'] ;
$price = $row['prix'] ;
$output[] = '<tr>';
$output[] = '<td class="t">'.$title.'</td>';
$output[] = '<td class="p">'.$price.' DT</td>';
$output[] = '<td class="q"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" class="center" />
</td>';
$output[] = '<td class="total">'.($price * $qty).' DT</td>';
$output[] = '<td class="d"><p><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="images/icones/supprimer.png" alt="supprimer" title="supprimer" /></a></p></td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '<tr>';
$output[] = '<td colspan="3"></td>';
$output[] = '<td class="right">Grand total:<br /> <strong>'.$total.' DT</strong></td>';
$output[] = '<td></td>';
$output[] = '</table>';
$output[] = '<div><button type="submit">Modifier la carte</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>* votre panier est vide.</p>';
}
return join('',$output);
}
?> |
page cart.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$action = $_GET['action'];
switch ($action) {
case 'add':
if (!empty($_SESSION['cart'])) {
$_SESSION['cart'] .= ','.$_GET['id'];
} else {
$_SESSION['cart'] = $_GET['id'];
}
break;
case 'delete':
if ($_SESSION['cart']) {
$items = explode(',',$_SESSION['cart']);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$_SESSION['cart'] = $newcart;
}
break;
case 'update':
if ($_SESSION['cart']) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$_SESSION['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;
}
}
}
}
}
$_SESSION['cart'] = $newcart;
break;
}
$cart = $_SESSION['cart'] ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP Shopping Cart Demo · Cart</title>
<link rel="stylesheet" href="css/styles.css" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include("masthead.php");
?>
<div id="content">
<div id="shoppingcart">
<h1 class="center">Votre panier</h1>
</div>
<div id="cart">
<h1 class="center">Bon de commande</h1><br />
<?php
echo showCart();
?>
</div>
<?php
$cart = ($_SESSION['cart']);
</div>
<?php
include("navbar.php");
include("footer.php");
?>
</body>
</html> |
Merci d'avance