<?PHPSession_Start();//Add the fruit to your shopping cart .$code=$_get["Code"];//$_session["GWC"];if(Empty($_session["GWC"])){ //1. If it is the first time to click on the shopping cart, do a two-dimensional array thrown into the session $attr=Array( Array($code, 1) ); $_session["GWC"] =$attr;}Else{ //determine if the fruit designator appears in the Sesson array $attr=$_session["GWC"]; if(Panduan ($code,$attr)) { //3. If it is not the first time you click Add to cart and the fruit is not the first click, add 1 to the number of the fruit in the Sesson array /*foreach ($attr as $v) {if ($code = = $v [0]) {$v [1] = $v [1]+1; } }*/ for($i= 0;$i<Count($attr);$i++) { if($code==$attr[$i][0]) { $attr[$i][1]++; } } $_session["GWC"] =$attr; } Else { //2. If the first click is not added to the cart and the fruit is clicked for the first time, make an array of the fruit and throw it into the Sesson's two-dimensional array. $arr=Array($code, 1); $attr[] =$arr; $_session["GWC"]=$attr; }}Var_dump($_session["GWC"]);//determine if V is present in ArrfunctionPanduan ($v,$arr){ $n= 0; foreach($arr as $a) { if($v==$a[0]) { $n++; } } if($n==0) { return false; } Else { return true; }}
1. If it is the first time to click on the shopping cart, make a two-dimensional array thrown into the session 2. If it is not the first time you click Add to cart and the fruit is first clicked, make an array of that fruit into the Sesson's two-dimensional array inside 3. If it is not the first time you click Add to cart and the fruit is not the first click, add 1 to the number of the fruit in the Sesson array
After output
Shopping Cart Purchase product operation