Problème avec array_keys

Par Vinkey G, il y a 5 ans


Bonjour, je suis entrain d'essayé de créé un panier depuis ma base de donnée, les données sont récupéré mais n'arrive pas a s'affiché et l'erreur qu'il m'affiche c'est : Warning: array_keys() expects parameter 1 to be array, string given in D:\Site\panierPHP\panier.php on line 27. pour moi il récupère l'id 0,1,2,3 etc... non!?
voici le code

<?php require 'header.php'; ?> <div class="checkout"> <div class="title"> <div class="wrap"> <h2 class="first">Shopping Cart</h2> </div> </div> <form method="post" action="panier.php"> <div class="table"> <div class="wrap"> <div class="rowtitle"> <span class="name">Product name</span> <span class="price">Price</span> <span class="quantity">Quantity</span> <span class="subtotal">Prix avec TVA</span> <span class="action">Actions</span> </div> <?php $search_array = array('premier' => 1, 'second' => 4); if (array_key_exists('premier', $search_array)) { echo "L'élément 'premier' existe dans le tableau"; } $ids = array_keys($_SESSION['panier']); if(empty($ids)){ $products = array(); }else{ $products = $DB->query('SELECT * FROM accesoires WHERE id IN ('.implode(',',$ids).')'); } foreach($products as $product): ?> <div class="row"> <a href="#" class="img"> <img src="img/<?= $product->id; ?>.jpg" height="53"></a> <span class="name"><?= $product->Articles; ?></span> <span class="price"><?= number_format($product->Prix_site,2,',',' '); ?> €</span> <span class="quantity"><input type="text" name="panier[quantity][<?= $product->id; ?>]" value="<?= $_SESSION['panier'][$product->id]; ?>"></span> <span class="subtotal"><?= number_format($product->Prix_site * 1.196,2,',',' '); ?> €</span> <span class="action"> <a href="panier.php?delPanier=<?= $product->id; ?>" class="del"><img src="img/del.png"></a> </span> </div> <?php endforeach; ?> <div class="rowtotal"> Grand Total : <span class="total"><?= number_format($panier->total() * 1.196,2,',',' '); ?> € </span> </div> <input type="submit" value="Recalculer"> </div> </div> </form> </div> <?php require 'footer.php'; ?>

et voici les classe panier

<?php class panier{ private $DB; public function __construct($DB){ if(!isset($_SESSION)){ session_start(); } if(!isset($_SESSION['panier'])){ $_SESSION['panier'] = array(); } $this->DB = $DB; if(isset($_GET['delPanier'])){ $this->del($_GET['delPanier']); } if(isset($_POST['panier']['quantity'])){ $this->recalc(); } } public function recalc(){ foreach($_SESSION['panier'] as $product_id => $quantity){ if(isset($_POST['panier']['quantity'][$product_id])){ $_SESSION['panier'][$product_id] = $_POST['panier']['quantity'][$product_id]; } } } public function count(){ return array_sum($_SESSION['panier']); } public function total(){ $total = 0; $ids = array_keys($_SESSION['panier']); if(empty($ids)){ $products = array(); }else{ $products = $this->DB->query('SELECT id, Prix_site FROM accesoires WHERE id IN ('.implode(',',$ids).')'); } foreach( $products as $product ) { $total += $product->Prix_site * $_SESSION['panier'][$product->id]; } return $total; } public function add($product_id){ if(isset($_SESSION['panier'][$product_id])){ $_SESSION['panier'][$product_id]++; }else{ $_SESSION['panier'][$product_id] = 1; } } public function del($product_id){ unset($_SESSION['panier'][$product_id]); } }

si quelqu'un pourait m'expliquer mes erreurs
merci,

1 réponse

SimonDevelop, il y a 5 ans

à la ligne $ids = array_keys($_SESSION['panier']);, ton $_SESSION['panier'] est une chaine de caractère, hors la fonction array_keys attend un tableau.

Fait un var_dump($_SESSION['panier']);die(); et essaye de faire le cheminement pour savoir pourquoi ton $_SESSION['panier'] est une chaine et non un tableau.
J'ai vite regarder ta classe panier, je n'ai rien de vu de problématique.