Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- //$_SESSION['panier'] = null;
- if (empty($_SESSION['panier'])) $_SESSION['panier'] = new Panier();
- if (!empty($_POST)) {
- $_SESSION['panier']->add_to_basket(key($_POST), 1);
- }
- $mesArticles = array();
- class Article {
- public $name;
- public $price;
- public function __construct($name, $price) {
- $this->name = $name;
- $this->price = $price;
- }
- public function toString() { return $this->name." (".$this->price." €)" ;}
- }
- class Panier {
- private $_panier;
- public function __construct() {$this->_panier = array(); }
- public function add_to_basket($obj_name, $quantite) {
- if (empty($this->_panier[$obj_name])) $this->_panier[$obj_name] = 1;
- else $this->_panier[$obj_name]++;
- }
- public function is_empty() { return empty($this->_panier); }
- public function print_panier() {
- echo "<pre>";
- print_r($this->_panier);
- echo "</pre>";
- }
- }
- $mesArticles['Stylo'] = new Article('Stylo', '5');
- $mesArticles['regle'] = new Article('regle', '1');
- $mesArticles['cahier'] = new Article('cahier', '3');
- $mesArticles['ciseaux'] = new Article('ciseaux', '2');
- $mesArticles['classeur'] = new Article('classeur', '9');
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Shopping Page</title>
- <style>
- table, th, td, tr {
- border: 1px solid black;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h1>Veuillez faire vos courses.</h1>
- <div class="form">
- <fieldset>
- <legend>Liste des Produits disponibles : </legend>
- <table style="width:100%">
- <th>Produits</th><th>Prix (€)</th><th>Add</th>
- <?php foreach($mesArticles as $article) { ?>
- <tr>
- <td><?= $article->name; ?></td>
- <td><?= $article->price; ?></td>
- <td><form name="<?= $article->name; ?>" method="POST" action="shopping.php">
- <input type="hidden" name="<?= $article->name; ?>" value="1">
- <input type="submit" value="Add to Basket">
- </form></td>
- </tr>
- <?php } ?>
- </table>
- </fieldset>
- </div>
- <?php if (!empty($_SESSION['panier'])) { $_SESSION['panier']->print_panier(); } ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement