Problème avec array_keys
vinkey33
Messages postés
92
Date d'inscription
Statut
Membre
Dernière intervention
-
yg_be Messages postés 23541 Date d'inscription Statut Contributeur Dernière intervention -
yg_be Messages postés 23541 Date d'inscription Statut Contributeur Dernière intervention -
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é sur la page panier 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!? et quand je fait un $search_array il me la trouve
voici le code
et voici la classe panier
si quelqu'un pourrait m'expliquer mes erreurs
merci,
voici le code
<?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; ?>
?>
et voici la classe panier
<?php
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;
}
si quelqu'un pourrait m'expliquer mes erreurs
merci,
2 réponses
yg_be
Messages postés
23541
Date d'inscription
Statut
Contributeur
Dernière intervention
Ambassadeur
1 584
bonjour, la première erreur, c'est, de ne pas tenir compte de notre demande relative à la spécification du langage PHP: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
je suppose que l'erreur se produit sur l'instruction
est-ce exact? dans ce cas, je te suggère de nous montrer ce que contient
je suppose que l'erreur se produit sur l'instruction
$ids = array_keys($_SESSION['panier']);
est-ce exact? dans ce cas, je te suggère de nous montrer ce que contient
$_SESSION['panier'].
oui mon erreur est sur array_keys qui fait tout buguer, j'ai fais un essaie avec
est me donne 0;b; a la place des articles mais il y a le même soucis sur array_keys dans le panier.class.php, il s'agit du total des article en nombre et somme, au lieu de m'afficher les totaux, il me signal les erreur suivante :
pour les totale des article
Warning: array_sum() expects parameter 1 to be array, string given in D:\Site\panierPHP\panier.class.php on line 32
pour la somme totale il me dit:
1 to be array, string given in D:\Site\panierPHP\panier.class.php on line 37
et voici panier.class.php
peut être que c'est liez!?
echo '<pre>'; print_r($_SESSION['panier']); echo '</pre>';
est me donne 0;b; a la place des articles mais il y a le même soucis sur array_keys dans le panier.class.php, il s'agit du total des article en nombre et somme, au lieu de m'afficher les totaux, il me signal les erreur suivante :
pour les totale des article
Warning: array_sum() expects parameter 1 to be array, string given in D:\Site\panierPHP\panier.class.php on line 32
pour la somme totale il me dit:
1 to be array, string given in D:\Site\panierPHP\panier.class.php on line 37
et voici panier.class.php
<?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 = 1; $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]); } }
peut être que c'est liez!?