Astuces php
Accueil
  News 
 Scripts 
 Bouquins 
 Newsletter 
  Emplois 
 Annuaire 
 Forum 
 
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 ...
 
 C'est ici
 
Avez vous lu ?
PHP 5 et MySQL 5
Avez vous lu ? -> PHP 5 et MySQL 5 : Présentation de l'éditeur
Ce livre explique comment se servir de PHP et de MySQL pour construire des sites Web dynamiques, interactifs et pratiques. Ce livre pratique inclut de nombreux exemples illustrant des tâches courantes comme l’authentification d’utilisateurs, la construction d’un caddie,...
 
 Commander
 
 Chapitres - Sessions
PHP 5 Astuces d'experts -> Sessions -> Stocker les informations d'authentification dans une session Stocker les informations d'authentification dans une session
PHP 5 Astuces d'experts -> Sessions -> Stocker le contenu d’une session Stocker le contenu d’une session
PHP 5 Astuces d'experts -> Sessions -> Gérer un panier Gérer un panier
 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,',',' ')
      . 
' &euro;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']) > 
' 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]
      . 
' &euro;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($total2','' ') . ' &euro;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");
}
?>
 Rechercher
Tapez un mot ou une phrase clé
 

 Téléchargement
Téléchargez les sources de l'ouvrage
PHP 5 solutions et composants open-source
Téléchargez les sources de l'ouvrage  -> PHP 5 solutions et composants open-source : Boostez et améliorez vos sites PHP avec des composants et extensions !

Voici enfin l'ouvrage pour améliorer votre site sans tout réécrire. Pourquoi réinventer la roue puisqu'il existe une multitude de composants indépendants à intégrer à votre site ou même des extensions au langage PHP qui peu...
 


Derniers tickets
  • bandeaux publicita... ()
  • Mail avec liste de... ()
  • pb install wamp ()
  • Bouton recalculer ()
  •  

    copyright astuces-php.info - Plan du site - Mylinea.org : l'annuaire des annuaires - échange de liens - Faites du fric