<?php session_start(); 
	include('conn.php');
?>
<!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=windows-874" />
<title>Untitled Document</title>
</head>

<body>
<?php
	if($_POST['button']!=""){
		$shopping=$_SESSION['shopping'];
		$number_shopping=$_SESSION['number_shopping'];
		$shopping_array = explode(",", $shopping);//แยกเครื่องหมาย
		$shopping_num=sizeof($shopping_array);//จำนวน เริ่ม 1
		if($_SESSION['shopping']==""){
			$_SESSION['shopping']=$_POST['id_product'];
			$_SESSION['number_shopping']=$_POST['shopping'];
			echo "<meta  http-equiv=\"Refresh\" content=\"0;url=sale.php\" />";
		}else{
			$_SESSION['shopping']=$_SESSION['shopping'].",".$_POST['id_product'];
			$_SESSION['number_shopping']=$_SESSION['number_shopping'].",".$_POST['shopping'];
			echo "<meta  http-equiv=\"Refresh\" content=\"0;url=sale.php\" />";
		}
	}else if($_POST['update']!=""){
		$shopping_update=$_POST['number_shopping'];
		$shopping_num_update=sizeof($shopping_update);

		for($num=0;$num<$shopping_num_update;$num++){
			if($num==0){
				$_SESSION['number_shopping']=$shopping_update[$num];
			}else{
				$_SESSION['number_shopping']=$_SESSION['number_shopping'].",".$shopping_update[$num];
			}	
		}
		echo "<meta  http-equiv=\"Refresh\" content=\"0;url=sale.php\" />";
	}else if($_POST['dalete']!=""){
		$_SESSION['number_shopping']="";
		$_SESSION['shopping']="";
		
		echo "<meta  http-equiv=\"Refresh\" content=\"0;url=sale.php\" />";
	}else{
		$shopping1=$_SESSION['shopping'];
		$shopping_array1 = explode(",", $shopping1);
		$number_shopping2=$_SESSION['number_shopping'];
		$shopping_array2 = explode(",", $number_shopping2);
		$shopping_num2=sizeof($shopping_array1);
		if($shopping_num2=="1"){
			$_SESSION['number_shopping']="";
			$_SESSION['shopping']="";
		}
		else{
			for($num2=0;$num2<$shopping_num2;$num2++){
				if($shopping_array1[$num2]==$_GET['id_product']){
					if($num2==0){
						$_SESSION['number_shopping']=$shopping_array2[$num2+1];
						$_SESSION['shopping']=$shopping_array1[$num2+1];
						$num2=$num2+1;
					}
				}else{
					if($num2==0){
						$_SESSION['number_shopping']=$shopping_array2[$num2];
						$_SESSION['shopping']=$shopping_array1[$num2];				
					}else{
						$_SESSION['number_shopping']=$_SESSION['number_shopping'].",".$shopping_array2[$num2];
						$_SESSION['shopping']=$_SESSION['shopping'].",".$shopping_array1[$num2];
					}	
				}
			}
		}
		echo "<meta  http-equiv=\"Refresh\" content=\"0;url=sale.php\" />";
		
	}
	//echo $shopping_num;
	//echo $shopping_array[0];
?>
</body>
</html>