 | | Offres d'emplois |  | Lead developer PHP On recherche un développeur-intégrateur adepte des bonnes pratiques du Web.
Mission :
- Participer à la conception technique des projets web
... | |
 | | Avez vous lu ? |  | | |
|
 Gérer un panier | |  |

<?php
session_start();
$action = (!isset($_REQUEST
['action']) ? '' : $_REQUEST['action']);
$tab_product = array(
$tab_product = array(
1=>array('prd_id'=>1,
'libelle'=>'Site web marchand PHP MySQL',
'prix'=>'32.00',
'description'=>'Réalisez votre boutique en ligne de A à Z'),
2=>array('prd_id'=>2,
'libelle'=>'La bible de la gravure',
'prix'=>'20.22',
'description'=>'Tous ce que vous devez
savoir sur la gravure Audio - Données - Vidéo'),
3=>array('prd_id'=>3,
'libelle'=>'La bible PHP',
'prix'=>'52.58',
'description'=>'La référence du PHP'),
4=>array('prd_id'=>4,
'libelle'=>'La bible SQL',
'prix'=>'58.20',
'description'=>'Tout sur SQL'));
if ($action == '') {
if (isset($_SESSION['panier']) &&
count($_SESSION['panier']) > 0) {
echo 'Votre panier contient :
<a href="' . $_SERVER['PHP_SELF'] . '
?action=view">' .
count($_SESSION['panier']) . ' article(s)
</a><br>';
}
$txt = '<table width="90%"
align="center" border="0"
cellspacing="5" cellpadding="0">';
for ($i=1;$i<=count($tab_product);$i++) {
$txt .= '<form name="produit_form"
action="'
. $_SERVER['PHP_SELF'] . '"
method="post">
<input type="hidden" name="prd_id"
value="'
. $tab_product[$i]['prd_id'] . '">
<input type="hidden" name="action"
value="add_kart">
<tr><td colspan="2" align="center"><b>'
. $tab_product[$i]['libelle'] . '</b></td></tr>';
$txt .= '<tr><td>' . $tab_product[$i]['description']
. '</td><td><b>'
. number_format($tab_product[$i]['prix'],2,',',' ')
. ' €uros</b></td></tr>';
$txt .= '<tr><td align="right" colspan="2">
<input type="submit"
name="submit"
value="Ajouter à votre
panier"></td></tr>';
$txt .= '<tr>
<td colspan="2" height="15">
</td></tr></form>';
}
$txt .= '</table>';
echo $txt;
}
elseif($action == 'add_kart') {
$quantite =
(isset($_POST['quantite']) ? $_POST['quantite'] : 1);
$new = intval($_POST['prd_id']) . ';' .
$quantite;
$_SESSION['panier'][] = $new;
header("Location: chapitre4b.php?action=view");
}
elseif($action == 'view' && isset
($_SESSION['panier'])) {
echo "Votre panier contient : "
. count($_SESSION['panier'])
. (count($_SESSION['panier']) > 1 ?
' articles' : ' article') . "<br>";
echo '<table width="90%"
align="center" border="0" cellspacing="5" cellpadding="0">
<tr><td align="center">
<b>Article</b></td>
<td><b>Quantité</b></td>
<td align="center"><b>Montant</b></td>
<td align="center"> </td></tr>';
for($j=0;$j<count($_SESSION['panier']); $j++) {
$tab_item = explode(";", $_SESSION['panier'][$j]);
$id_prd = intval($tab_item[0]);
$q = intval($tab_item[1]);
$total_ligne[] =
number_format(($q * floatval($tab_product[$id_prd]['prix'])), 2);
echo '<form action="' . $_SERVER['PHP_SELF']
. '" method="post">';
echo '<input type="hidden"
name="action" value="effacer">';
echo '<input type="hidden"
name="item" value="' . $j .'">';
echo '<tr><td align="center">'
. $tab_product[$id_prd]['libelle'] . '</td>';
echo '<td align="center">' . $q . '</td>';
echo '<td align="center">' . $total_ligne[$j]
. ' €uros</td>';
echo '<td align="right">
<input type="submit" name="submit" value="Supprimer">
</form></td></tr>';
}
$total = array_sum($total_ligne);
echo '<tr><td align="right" colspan="2">
Total du panier :</td>
<td align="center">'
. number_format($total, 2, ',', ' ') . ' €uros</td>
<td> </td></tr>';
echo '</table>';
echo '<center><a href="' . $_SERVER['PHP_SELF']
. '">Retour à la liste des articles</a></center>';
}
elseif($action == 'view' && !isset($_SESSION['panier'])) {
echo '<center>Votre panier est vide<br>';
echo '<a href="' . $_SERVER['PHP_SELF']
. '">Retour à la liste des articles</a></center>';
}
elseif($action == 'effacer') {
$temp_array['panier'] = $_SESSION['panier'];
unset($_SESSION['panier']);
foreach ($temp_array['panier'] as $key => $value) {
if($key != intval($_POST['item'])) {
$_SESSION['panier'][] = $temp_array['panier'][$key];
}
}
header("Location: chapitre4b.php?action=view");
}
?>
|
|
|
 |
|