Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4. //$_SESSION['panier'] = null;
  5.  
  6. if (empty($_SESSION['panier'])) $_SESSION['panier'] = new Panier();
  7.  
  8. if (!empty($_POST)) {
  9.     $_SESSION['panier']->add_to_basket(key($_POST), 1);
  10. }
  11.  
  12. $mesArticles = array();
  13.  
  14. class Article {
  15.    
  16.     public $name;
  17.     public $price;
  18.    
  19.     public function __construct($name, $price) {
  20.         $this->name = $name;
  21.         $this->price = $price;
  22.     }
  23.    
  24.     public function toString() { return $this->name." (".$this->price." €)" ;}
  25.  
  26. }
  27.  
  28. class Panier {
  29.    
  30.     private $_panier;
  31.    
  32.     public function __construct() {$this->_panier = array(); }
  33.    
  34.     public function add_to_basket($obj_name, $quantite) {
  35.        
  36.         if (empty($this->_panier[$obj_name])) $this->_panier[$obj_name] = 1;
  37.         else $this->_panier[$obj_name]++;
  38.     }
  39.    
  40.     public function is_empty() { return empty($this->_panier); }
  41.    
  42.     public function print_panier() {
  43.         echo "<pre>";
  44.         print_r($this->_panier);
  45.         echo "</pre>";
  46.     }
  47. }
  48.  
  49. $mesArticles['Stylo'] = new Article('Stylo', '5');
  50. $mesArticles['regle'] = new Article('regle', '1');
  51. $mesArticles['cahier'] = new Article('cahier', '3');
  52. $mesArticles['ciseaux'] = new Article('ciseaux', '2');
  53. $mesArticles['classeur'] = new Article('classeur', '9');
  54.  
  55. ?>
  56.  
  57. <!DOCTYPE html>
  58. <html>
  59. <head>
  60.   <title>Shopping Page</title>
  61.   <style>
  62. table, th, td, tr {
  63.     border: 1px solid black;
  64.     text-align: center;
  65. }
  66. </style>
  67. </head>
  68.  
  69. <body>
  70.   <h1>Veuillez faire vos courses.</h1>
  71.  
  72. <div class="form">
  73.   <fieldset>
  74.         <legend>Liste des Produits disponibles : </legend>
  75.  
  76.     <table style="width:100%">
  77.         <th>Produits</th><th>Prix (€)</th><th>Add</th>
  78.        
  79.  
  80.     <?php foreach($mesArticles as $article) { ?>
  81.         <tr>
  82.             <td><?= $article->name; ?></td>
  83.             <td><?= $article->price; ?></td>
  84.             <td><form name="<?= $article->name; ?>" method="POST" action="shopping.php">
  85.                     <input type="hidden" name="<?= $article->name; ?>" value="1">
  86.                     <input type="submit" value="Add to Basket">
  87.                 </form></td>
  88.         </tr>
  89.     <?php } ?>
  90.  
  91.  
  92.   </table>
  93.  
  94.   </fieldset>
  95.  </div>
  96.    
  97. <?php if (!empty($_SESSION['panier'])) { $_SESSION['panier']->print_panier(); } ?>
  98.  
  99.  
  100. </body>
  101.  
  102. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement