8796 sujets

Développement web côté serveur, CMS

Bonjour,

je commence le "panier" via les SESSIONS.

J'ai fais pour commencer un exemple assez simple. J'arrive à ajouter des éléments, à vider l'entièreté de mon panier. Par contre j'ai un problème pour afficher le contenu. Il m'affiche "Array" au lieu de l'intitulé de chaque produits. J'ai également un problème qui est sans doute lié à cela c'est que je n'arrive pas à supprimer un élément.

Voici mon code, si quelqu'un à une idée.

D'avance merci

Mes fonctions



/* ------------------------ */
/* Déclaration de Fonctions */
/* 		  Pour panier		*/
/* ------------------------ */

/* => Fonction ajout d'un produit <= */
function ajout($produit)
	{
	$new_prod['produit']=$_GET['pro_nom'];
		$new_prix['prix']=$_GET['prix'];
		echo '<p>Produit <u>'.$new_prod['produit'].'</u> a été ajouté</p>';
		array_push($_SESSION['panier']['produit'],$new_prod);
		array_push($_SESSION['panier']['prix'],$new_prix);
	}

/* => Vider le panier <= */
function vider()
	{
	echo '<p>Le panier est vidé</p>';
		unset($_SESSION['panier']);
	}


/* => Calcul contenu du panier <= */
function total_panier()
	{
	 if(!empty($_SESSION['panier']['produit']))
	 {
		$panier=count($_SESSION['panier']['produit']);
		return $panier;
	 }
	 else
	 {
	 $panier=0;
	 return $panier;
	 }
	}


/* => Affichage du panier <= */
function affiche_panier()
	{
	echo '<table id="tableau" border="1" width="500px" height="300px">';
        echo '<thead>';
           echo '<tr>';
               echo '<th scope="col" id="">Produits</th>';
               echo '<th scope="col" id="">Prix</th>';
               echo '<th scope="col" id="">Choix</th>';
            echo '</tr>';
       echo '</thead>';
        echo '<tbody>';
$panier=count($_SESSION['panier']['produit']);
for ($i=0; $i<$panier; $i++)
	{
 	 		echo '<tr>';

                echo '<td>'.$_SESSION['panier']['produit'][$i].'</td>';
                echo '<td>'.$_SESSION['panier']['prix'][$i].'</td>';
                echo '<td><a href="'.$_SERVER['PHP_SELF'].'?choix=supprimer&amp;produit='.$_SESSION['panier']['produit'][$i].'">Supprimer cet article</a></td>';
            echo '</tr>';
	}
        echo '</tbody>';
  echo '</table>';	

	}	
	


Ma page ou je vois mes produits et ou je peux en ajouter ou vider mon panier.



session_start(); 

/*On vérifie si le panier existe*/
if(!isset($_SESSION['panier']))
	{
	/* Initialisation du panier */ 
	$_SESSION['panier'] 			= array();

	/*Subdivision du panier*/
	$_SESSION['panier']['produit']	= array();
	$_SESSION['panier']['prix']		= array();
	}


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Document sans nom</title>
</head>

<body>
    <p>Liste de nos produits</p>


include('connex.inc.php');
        $idcom=connex('panierdim', 'myparam');
        mysql_query("SET NAMES utf8"); /*TRES IMPORTANT -> permet d'éviter les problèmes d'affichage d'accents*/
include('function.inc.php');



/* En Cas d'ajout d'article */
/* ------------------------ */

	if (isset($_GET['pro_nom']))
		{
		$produit=$_GET['pro_nom'];
		ajout($produit);
		}

/*     Vider le panier      */
/* ------------------------ */
		
	if (isset($_GET['choix'])=="vider")
		{
		vider();
		}		

echo '<p>Votre panier contient actuellement '.total_panier().' articles</p>';
$panier=total_panier();
/* Requete à la base */
$request_products=mysql_query("SELECT pro.pro_id, pro.pro_nom, pro.prix
							   FROM t_produits_pro pro ORDER BY pro.pro_nom ASC") or die ("Sélection de la base impossible");

/* Tableau de produits + prix */							   
   echo '<table id="tableau" border="1" width="500px" height="300px">';
        echo '<thead>';
           echo '<tr>';
               echo '<th scope="col" id="">Produits</th>';
               echo '<th scope="col" id="">Prix</th>';
               echo '<th scope="col" id="">Choix</th>';
            echo '</tr>';
       echo '</thead>';
        echo '<tbody>';
	while ($donnees_products=mysql_fetch_array($request_products))
		{
	 		echo '<tr>';
                echo '<td>'.$donnees_products['pro_nom'].'</td>';
                echo '<td>'.$donnees_products['prix'].'</td>';
                echo '<td><a href="'.$_SERVER['PHP_SELF'].'?pro_nom='.$donnees_products['pro_nom'].'&amp;prix='.$donnees_products['prix'].'" id="ajout" ">Ajouter au panier</a></td>';
            echo '</tr>';
		}

              </tbody>
    	</table>
<!--FIN DE TABLEAU-->
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?choix=vider">Vider mon panier</a></p>
<!--  <p><a href="index.php?choix=vider">Vider mon panier</a></p>    -->

 <p><a href="panier.php">Terminer mes achats et consulter mon panier</a></p>   
if(!empty($_SESSION['panier']) AND $panier!=0)
	{echo '<pre>'; var_dump($_SESSION['panier']); echo '</pre>';
	echo '<br/>';
	print_r($_SESSION['panier']);
	}

</body>
</html>



Et enfin ma page panier qui devrait afficher l'intitulé de chacun des produits mis dans le panier. Le nombre d'éléments est lui bon.


<?php
session_start(); 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Document sans nom</title>
</head>

<body>


include('function.inc.php');

if(!empty($_SESSION['panier']['produit']))
	{
	$panier=count($_SESSION['panier']['produit']);
	var_dump($_SESSION['panier']['produit']);
	}
	else
	{
	$panier='0';
	}
echo '<p>Contenu de votre commande : ' .$panier.' articles </p>';
/* affiche_panier(); */
echo '<table id="tableau" border="1" width="500px" height="300px">';
        echo '<thead>';
           echo '<tr>';
               echo '<th scope="col" id="">Produits</th>';
               echo '<th scope="col" id="">Prix</th>';
               echo '<th scope="col" id="">Choix</th>';

            echo '</tr>';
       echo '</thead>';
        echo '<tbody>';

for ($i=0; $i<$panier; $i++)
	{
 	 		echo '<tr>';
 	 			$produit=$_SESSION['panier']['produit'][$i];
 	 			$prix=$_SESSION['panier']['prix'][$i];
                echo '<td>'.$_SESSION['panier']['produit'][$i].'</td>';
                echo '<td>'.$_SESSION['panier']['prix'][$i].'</td>';
                echo '<td><a href="'.$_SERVER['PHP_SELF'].'?choix=supprimer&amp;produit='.$_SESSION['panier']['produit'].'">Supprimer cet article</a></td>';
            echo '</tr>';
	}
        echo '</tbody>';
  echo '</table>';	

/* ------------------------------- */	
/* En Cas de suppression d'article */
/* ------------------------------- */	
if (isset($_GET['choix'])=="supprimer")
	{
	$suppression=false;
	$suppr_produit=$_GET['produit'];
	echo '<p>Le produit '.$suppr_produit.' a été supprimé</p>';
	
	/* Stock contenu du panier sans celui à supprimer */
	$panier_tmp=array("produit"=>array(), "prix"=>array());
	
	/* Comptage des articles du panier */
	$nb_produits=count($_SESSION['panier']['produit']);
	for($i=0; $i<$nb_produits; $i++)
		{
		if($_SESSION['panier']['produit'][$i]!=$suppr_produit)
			{
			array_push( $panier_tmp['produit'], $_SESSION['panier']['produit'][$i]);
			array_push( $panier_tmp['prix'], $_SESSION['panier']['prix'][$i]);
			}
		}
	/* Le transfert est terminé on réinitialise le panier */
	$_SESSION['panier']=$panier_tmp;
	/* Suppression du panier temporaire */
	unset($panier_tmp);
	$suppression=true;
	return $suppression;
	echo '<p><a href="index.php">Continuer mes achats</a></p>';
	}	
echo '<p><a href="index.php">Continuer mes achats</a></p>';


</body>
</html>


Modifié par paintbox (23 Nov 2010 - 00:03)
Bonjour...

Déjà une simple relecture de ton poste t'aurais permis de voir que tout ce qui est PHP n'apparait pas.. pas facile de t'aider dans ces conditions...

Supprime les balises d'ouvertures et de fermetures du php... ça apparaitra
Salut...

J'ai pas trop le temps là...

mais dans un premier temps, une session sur laquelle on travaille devrait en quelque sorte etre gérée comme une BDD...

DOnc moi j'aurai plutot travaillé avec des id un truc de ce genre

$_SESSION[ panier ][0][Produit]
$_SESSION[ panier ][0][Prix]
$_SESSION[ panier ][0][QTe]
$_SESSION[ panier ][1][Produit]
$_SESSION[ panier ][1][Prix]
$_SESSION[ panier ][1][QTe]

etc etc...

Facile d'avoir l'id lors de l'affichage et donc de travailler dessus dans la session...
Modifié par pchlj (23 Nov 2010 - 11:47)