<?php

		session_start();

		if($_SESSION['database']=="local")
	  {
	     $db_host    = "localhost";
	     $db_user   = "root";
	     $db_pass   = "";
	     $db_name   = "boutique";	
	 }
	
	if($_SESSION['database']=="public")
	  {
  	     $db_host    = "sql2";
   	     $db_user   = "analogde";
 	     $db_pass   = "rainbow";
    	     
	     $db_name   = "analogde";
	     //$db_type   = "mysql";	
	 }
	
			 
    				/* Initialisation du panier */
    				$_SESSION['panier'] = array();
    				/* Subdivision du panier */
    				$_SESSION['panier']['id'] = array();
    				$_SESSION['panier']['qte'] = array();
    				$_SESSION['panier']['designation'] = array();
    				$_SESSION['panier']['prix_ttc'] = array();
    				$_SESSION['panier']['prix_ht'] = array();
    				 
			
	
	

	

	if(isset($_GET['action']))
	  {
	     if($_GET['action'] == "charge" ) 
               {  
        		$indice = $_GET['elem'];
			
			//$connexion = mysql_connect("localhost", "root", "");
	                $connexion = mysql_connect($db_host,$db_user,$db_pass);
	                mysql_select_db($db_name, $connexion);

			
			$requete = "SELECT * FROM panier WHERE id='$indice' ";
     			$result = mysql_query($requete);
			$panier_liste=mysql_fetch_array($result);
			
			//print_r($panier_liste);
			
	    		$articles = explode("," , $panier_liste['panier']);
	  	     	
	  	     	//echo "+++++";
	  	     	print_r($articles);
	  	     	
	  	     	$nb_articles = count($articles)/4; 
			
			echo $nb_articles;
			
			 // 0 3 6 
			 
			
			 
			$ptr = 0;
			 
			//NTH82K, lave_linge, 3, 1438.5, X28Z-22, 3, 2164.5 
			// 0      1    2      3       4   5 
			
				
				
				//$connexion = mysql_connect("localhost", "root", "");
       		   	        $connexion = mysql_connect($db_host,$db_user,$db_pass);
       		   	        mysql_select_db($db_name, $connexion);


				$ptr = 0;
				
				for($i=0;$i<$nb_articles;$i++)
			           {
				      $ref = $articles[$ptr];
				      $ref = trim($ref);
				        	$requete = "SELECT * FROM lave_linge WHERE modele='$ref'";
			                	$result = mysql_query($requete);
				        	$data  = mysql_fetch_assoc($result);
		
				        	$select['id'] = $data['id'];
	      			        	$select['qte'] = $articles[$ptr+2];
				        	$select['designation'] = $data['modele'];	
				        	$select['prix_ttc'] = $data['prix_ttc'];
				        	$select['prix_ht'] = $data['prix_ht'];
				        	ajout($select);
				
					$ptr = $ptr +4;
				}	
				
				var_dump($_SESSION);
				
				 
				 
		        
			
			
			// affiche le contenu du tableau panier stocké dans la variable globale $_SESSION
			
			
			 //header('Location: ajouter_panier.php'); 
	  }	

	}

	
?>


<?php
function ajout($select)
{
    array_push($_SESSION['panier']['id'],$select['id']);
    array_push($_SESSION['panier']['qte'],$select['qte']);
    array_push($_SESSION['panier']['designation'],$select['designation']);
    array_push($_SESSION['panier']['prix_ttc'],$select['prix_ttc']);
    array_push($_SESSION['panier']['prix_ht'],$select['prix_ht']);
} 

?>	