<?php

	session_start();
	
	include('../config_inc.php'); 
	
	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";	
	 }
	
		      if(!isset($_SESSION['panier']))
	  		{
				//initialisation du panier 
    	     		  	$_SESSION['panier'] = array();
    	     			//subdivision du panier 
  	     			$_SESSION['panier']['id_catalogue'] = array();
    	     			$_SESSION['panier']['reference'] = array();
	     			$_SESSION['panier']['marque'] = array();
	     			$_SESSION['panier']['table'] = array();
    	     			$_SESSION['panier']['qte'] = array();
    	     			$_SESSION['panier']['designation'] = array();
    	     			$_SESSION['panier']['prix_ttc'] = array();
    	     			$_SESSION['panier']['prix_ht'] = array();
    	     			$_SESSION['panier']['dispo'] = array(); 
   	  		}
		   	

		   	if(isset($_GET['caddie']))
		   	  {
		   	        $ref = $_GET['ref']; 
		   	        $caddie = $_GET['caddie']; 
		   	        $table = $_GET['table']; 

		   		if( $caddie=="ajout" )
	                  	  {    if(in_array($ref, $_SESSION['panier']['reference']))
	  	                  	  { $position = array_search($ref, $_SESSION['panier']['reference']);
	 	                    	    $_SESSION['panier']['qte'][$position] = $_SESSION['panier']['qte'][$position] + $_POST['qte'];
		       	            	    //header('Location: rafraichir_panier02.php');
		       	          	  }  		
	                  	
	                  		else {  $connexion = mysql_connect($db_host,$db_user,$db_pass); 
       		   	                	mysql_select_db($db_name, $connexion);

				        	$requete = "SELECT * FROM $table WHERE reference='$ref'";
			                	$result = mysql_query($requete);
				        	$data  = mysql_fetch_assoc($result);
		
						if(isset($_POST['qte']))
	 	                    	          { $quantite = $_POST['qte'];
	 	                    	          }	
	 	                    	    else { $quantite = 1;
	 	                    	    	 }  
		
						$select['id_catalogue'] = $data['id_catalogue'];
				        	$select['reference'] = $data['reference'];
				        	$select['marque'] = $data['marque'];
				        	$select['table'] = $table;
	      			        	$select['qte'] = $quantite ; //$_POST['qte'];
				        	$select['designation'] = $data['designation'] ." " .$data['marque'] ." " .$data['reference'];	
				        	$select['prix_ttc'] = $data['prix_ttc'];
				        	$select['prix_ht'] = $data['prix_ht'];
				        	$select['dispo'] = $data['dispo'];
				        	
				        	ajout($select);
				        	//header('Location: rafraichir_panier02.php');

		                    	}		  	   
		   	         }
		   	
		   	}	
			
			           

			$contenu = count($_SESSION['panier']['reference']);

			$compte_articles = 0;

			if($contenu != 0)
	  		  { for($i=0;$i<count($_SESSION['panier']['reference']);$i++)
	       		       { $compte_articles = $compte_articles +  $_SESSION['panier']['qte'][$i];
	       		         $total_ttc+=$_SESSION['panier']['prix_ttc'][$i]*$_SESSION['panier']['qte'][$i];
	       		       }
			  }

			$_SESSION['nb_article_panier'] = $compte_articles;
			$_SESSION['total_ttc_panier'] = $total_ttc;
			
			$_SESSION['boite_message']= "ok";
			
			//echo "<pre>"; 
        		//var_dump($_SESSION);
        		//echo "</pre>";
        
        		//header($_SERVER['HTTP_REFERER']); 

			header("Location: " . $_SERVER["HTTP_REFERER"]);


			//header('Location: ../catalogue/visu02_produit.php?ref="$ref"&table="$table"');

			//header('Location: toto.php'); 

			//visu02_produit.php?ref=$ref&table=$table

        
        		/*echo "
        			<a href=\"javascript:history.back()\"></a> ";*/

?>

<?php
function ajout($select)
{
    array_push($_SESSION['panier']['id_catalogue'],$select['id_catalogue']);	
    array_push($_SESSION['panier']['reference'],$select['reference']);
    array_push($_SESSION['panier']['marque'],$select['marque']);
    array_push($_SESSION['panier']['table'],$select['table']);
    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']);
    array_push($_SESSION['panier']['dispo'],$select['dispo']);
	
} 

?>	