[PHP] afficher avec une boucle un array

Résolu
okuni Messages postés 1325 Statut Membre -  
Alain_42 Messages postés 5413 Statut Membre -
Bonjour,

Je suis en train de créer un panier virtuel pour mon site et j'aimerais savoir comment faire pour afficher tout le panier virtuel.
Il est construit dans un array en session comme suit :
/* Initialisation du panier */
    $_SESSION['panier'] = array();
    /* Subdivision du panier */
    $_SESSION['panier']['id_produit'] = array();
    $_SESSION['panier']['nom_produit'] = array();
    $_SESSION['panier']['quantite'] = array();
    $_SESSION['panier']['taille'] = array();
    $_SESSION['panier']['couleur'] = array();
    $_SESSION['panier']['prix'] = array();

Sur une page, j'aimerais donc afficher tout le contenu de l'array à l'aide d'un while sauf que je ne sais pas quoi mettre comme condition pour le while.

Quelqu'un sait m'aider?
--
L'amour, c'est comme les spaghettis; quand c'est mou, c'est cuit. (proverbe belge)

9 réponses

  1. giheller
     
    Bonjour,

    voir si :
    $limite = sizeof( $_SESSION['panier']);
    ne donne pas le nombre de ligne.
    0
  2. okuni Messages postés 1325 Statut Membre 126
     
    Sa ne marche pas tout a fait.
    voici le résultat :
    J'ai ajouter 2 séries d'articles
    donc 2 fois le prix, la taille,...
    Nom  	Prix unitaire  	Quantité  	Prix total  	Supprimer
    test5 	50 € 		200 € 	Supprimer
    test5 	50 € 		100 € 	Supprimer
    	€ 		0 € 	Supprimer
    	€ 		0 € 	Supprimer
    	€ 		0 € 	Supprimer
    	€ 		0 € 	Supprimer
    	€ 		0 € 	Supprimer

    Par observation, je dirais que la fonction que tu m'as donné compte le nombre de ligne seulement dans l'array panier et pas ceux des autres array qui sont dans l'array panier comme par exemple l'array id qui contient 2 id (qui s'affiche ici).

    EDIT : après avoir chipotter un peu, j'ai réussi a adapter ta fonction.
    merci pour ton aide :D
    0
    1. giheller
       
      Ok mais regarder alors côté foreach ($_SESSION['panier']
      0
    2. Alain_42 Messages postés 5413 Statut Membre 904
       
      en complément:

      /* Initialisation du panier */
          $_SESSION['panier'] = array();
          /* Subdivision du panier */
          $_SESSION['panier']['id_produit'] = array();
          $_SESSION['panier']['nom_produit'] = array();
          $_SESSION['panier']['quantite'] = array();
          $_SESSION['panier']['taille'] = array();
          $_SESSION['panier']['couleur'] = array();
          $_SESSION['panier']['prix'] = array();
      	
      	
      	//il faut faire deux boucles inbriquees
      	
      	foreach($_SESSION['panier'] as $cle1=>$s_array){
      		//tu parcours l'array $_SESSION['panier']
      		echo "<br />Contenu de ".$cle1;"<br />";
      		foreach($s_array as $cle2=>$value){
      			// la tu parcours les sous array 
      			echo $value."<br />";
      		}
      	}
      0
  3. okuni Messages postés 1325 Statut Membre 126
     
    Si j'utilise un foreach, je ne pourrais pas afficher plusieurs donnée différentes sur une même ligne.
    Car mon code n'est pas prévu pour mettre un foreach.
    voici mon code :
    foreach ($_SESSION['panier'] as $key1=>$array)
    		{
    			foreach ($array as $key2=>$value)
    			{
    				?>
    					<tr>
    						<td><?php echo $_SESSION['panier'][$key2][$value];?></td>
    						<td><?php echo $_SESSION['panier'][$key2][$value].' €';?></td>
    						<td><?php echo '<input type="text" style="text-align:center;" class="formulaire" name="nombre_produit' . $numLine . '" value="'.$_SESSION['panier'][$key2][$value].'" />';?></td>
    						<td><?php echo $_SESSION['panier'][$key2][$value] * $_SESSION['panier'][$key2][$value].' €';?></td>
    						<td><a href="?page=commander&mod=<?php echo $_SESSION['panier'][$key2][$value];?>">Supprimer</a></td>
    					</tr>
    				<?php
    				$prix = $_SESSION['panier'][$key2][$value] * $_SESSION['panier'][$key2][$value];
    				$prixtot = $prix + $prixtot;
    				
    				//Incrémentation
    				$numLine++;
    			}
    		}

    Comme on peut le voir, j'ai un peu bidouiller pour l'utilisation du foreach mais sa ne marche pas, je m'embrouille avec les sessions et les foreach.
    et forcément si je met partout la variable $variable, je n'aurai pas du tout ce qu'il me faut.
    0
  4. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  5. Alain_42 Messages postés 5413 Statut Membre 904
     
    Oui tu n'as pas saisi le principe du foreach

    cette boucle parcours un array et à chaqu epassage associe l'index $cle avec la valeur $value

    donc tu peux travailler dessu à chaque passage avec ces variables

    dans ton cas si tu connais ce qu'il y a dans le sous array pas besoin de 2 ième boucle

    <?php
    $numLine=1;
    foreach ($_SESSION['panier'] as $key1=>$array){
    	//si tu sais ce qu'il y a dans ce sous array pas besoin de 2 ieme boucle
    	$nom=$array[0];
    	$prix_unit=$array[1];
    	$quantite=$array[2];
    	$prix_total=$array[3];
    	$supprimer=$array[3];
    			
    			
    			
    				?>
    					<tr>
    						<td><?php echo $nom;?></td>
    						<td><?php echo $prix_unit.' &euro;';?></td>
    						<td><?php echo '<input type="text" style="text-align:center;" class="formulaire" name="nombre_produit' . $numLine . '" value="'.$quantite.'" />';?></td>
    						<td><?php echo $prix_unit * $quantite.' €';?></td>
    						<td><a href="?page=commander&mod=<?php echo $nom;?>">Supprimer</a></td>
    					</tr>
    				<?php
    								
    				//Incrémentation
    				$numLine++;
    }
    0
  6. okuni Messages postés 1325 Statut Membre 126
     
    Très bizarre, voici mon résultat :
    Nom  	Prix unitaire  	Quantité  	Prix total  	Supprimer
    4 	€ 		0 € 	Supprimer
    test5 	€ 		0 € 	Supprimer
    4 	€ 		0 € 	Supprimer
    s 	€ 		0 € 	Supprimer
    s 	€ 		0 € 	Supprimer
    50 	€ 		0 € 	Supprimer
    0
    1. Alain_42 Messages postés 5413 Statut Membre 904
       
      quel est le contenu des array ? nom, prix unit etc..

      est ce que tu les remplis pour chaque index, même si vide ?
      0
    2. okuni Messages postés 1325 Statut Membre 126 > Alain_42 Messages postés 5413 Statut Membre
       
      Chaque array contient une donnée, aucun champ n'est vide et il y a 6 array différent dans l'array panier
      $_SESSION['panier']['id_produit']
      	    $_SESSION['panier']['nom_produit']
      	    $_SESSION['panier']['quantite']
      	    $_SESSION['panier']['taille']
      	    $_SESSION['panier']['couleur']
      	    $_SESSION['panier']['prix']

      voila les 6 arrays différents.
      0
    3. Alain_42 Messages postés 5413 Statut Membre 904 > okuni Messages postés 1325 Statut Membre
       
      Oui mais au niveau en dessous ?

      /* Initialisation du panier */
      $_SESSION['panier'] = array();
      /* Subdivision du panier */
      $_SESSION['panier']['id_produit'] = array();
      $_SESSION['panier']['nom_produit'] = array();
      $_SESSION['panier']['quantite'] = array();
      $_SESSION['panier']['taille'] = array();
      $_SESSION['panier']['couleur'] = array();
      $_SESSION['panier']['prix'] = array();


      que contieneent les array() ainsi définis
      0
    4. okuni Messages postés 1325 Statut Membre 126 > Alain_42 Messages postés 5413 Statut Membre
       
      Je ne comprend pas très bien.
      a ce moment dans le code, comme les array sont crééent, elle sont vide mais des que je rajoute une donnée (ajout d'un t-shirt) toute les array sont complétées en même temps.
      0
    5. Alain_42 Messages postés 5413 Statut Membre 904 > okuni Messages postés 1325 Statut Membre
       
      elle sont vide mais des que je rajoute une donnée (ajout d'un t-shirt) toute les array sont complétées en même temps.

      comment écris tu dans ces array ? par un index numéroté de 0 à ...

      ou par un index chaine par exemple prix_unitaire
      0
  7. Alain_42 Messages postés 5413 Statut Membre 904
     
    en complement:

    $prix_total=$prix_unit*$quantite;
    $_SESSION['panier']['id_produit'] = array("nom"=>"toto","prix_unit"=>"20","quantite"=>"5","prix_total"=>"".$prix_total."","supprimer"=>"supprimer");
    
    //ce qui donnera pour l'affichage:
    
    <?php
    $numLine=1;
    foreach ($_SESSION['panier'] as $key1=>$array){
    	//si tu sais ce qu'il y a dans ce sous array pas besoin de 2 ieme boucle
    	$nom=$array['nom'];
    	$prix_unit=$array['prix_unit'];
    	$quantite=$array['quantite'];
    	$prix_total=$array['prix_total'];
    	$supprimer=$array['supprimer'];
     //etc...
    0
  8. Alain_42 Messages postés 5413 Statut Membre 904
     
    Bonsoir,

    Essayes comme ça, j'aienfin compris comment sont organisés tes array

    <?php
    /* Initialisation du panier */
        $_SESSION['panier'] = array();
        /* Subdivision du panier */
        $_SESSION['panier']['id_produit'] = array();
        $_SESSION['panier']['nom_produit'] = array();
        $_SESSION['panier']['quantite'] = array();
        $_SESSION['panier']['taille'] = array();
        $_SESSION['panier']['couleur'] = array();
        $_SESSION['panier']['prix'] = array();
    	
    	
    	// en fait il faut afficher les sous array
    	
    	// taille des sous array: (on part du principe qu'ils ont tous la même longueur)
    	
    	$nb_index=sizeof($_SESSION['panier']['id_produit']); 
    	
    	//par une boucle on les parcours en affichant leur valeur
    	for($i=0;$i<$nb_index;$i++){
    ?>
    		<tr>
    			<td><?php echo $_SESSION['panier']['nom_produit'][$i];?></td>
    			<td><?php echo $_SESSION['panier']['prix'][$i].' €';?></td>
    			<td><?php echo '<input type="text" style="text-align:center;" class="formulaire" name="nombre_produit'.$i. '" value="'.$_SESSION['panier']['quantite'][$i].'" />';?></td>
    						<td><?php echo $_SESSION['panier']['prix'][$i] * $_SESSION['panier']['quantite'][$i].' €';?></td>
    						<td><a href="?page=commander&mod=<?php echo $_SESSION['panier']['id_produit'][$i];?>">Supprimer</a></td>
    					</tr>
    
    	
    	<?php
    	
    	}
    	
    	?>


    @lain
    0
  9. okuni Messages postés 1325 Statut Membre 126
     
    Ok merci :)
    mais tu n'utilise pas foreach dans cet exemple.
    0
    1. Alain_42 Messages postés 5413 Statut Membre 904
       
      Non ,

      foreach est intéressant si on veut utiliser le nom de l'index dans le cas d'un array avec index non numerique (string)

      et de plus dans ton cas foreach ne parcourrait qu'un seul des array, hors la on affiche un index de chacun des array à chaque boucle
      0