Add items to the shopping cart add_to_cart.php

? <PHP
		 session_start (); // start the session
		 
		$ goods_id = $ _POST [ 'goods_id']; // get goods the above mentioned id 
		
		$ = Number The $ _POST [ 'NUM']; // get goods number 

		if (empty ($ _ SESSION [ "GWC"])) 
		{ 

		    // 1 shopping cart is empty, first click Add cart. 

		    $ arr [$ goods_id] = Array ( 
		    				'goods_id' => $ goods_id, 
		    				
		    				'NUM' => $ Number the 
		    				
		    				); 

		    _SESSION $ [ "GWC"] = $ ARR; 

		} 
		the else // _. 1 cart is not empty 
		{ 

		    

		    // determines whether the presence of the product cart 

		    $ arr = $ _SESSION [ "gwc "]; // preexisting about 

		    $ chuxian false =; 

		    . 2 // query whether there is an article in the shopping cart, K is the product id, make a mark 
		    foreach ($ arr as $ k = >$v)
		    {
		    	
		        if($k==$goods_id)
		        {

		            to true chuxian = $; 

		        } 

		    } 

		 
		// 3. This product has the cart, it will simply increase the number of 
		    IF ($ chuxian) 
		    { 

		    	$ ARR [$ goods_id] [ 'NUM'] + = $ Number; 
		        
		        $ _SESSION [ "GWC"] = $ ARR; 
		    } 
		    the else 
		    { 

		        . // _. 3 If the shopping cart is the product 
		        $ ARR [$ goods_id] = Array ( 
		    				'goods_id' => $ goods_id,		    				 
		    				'NUM' => $ Number		    				 
		    				);		          
		        _SESSION $ [ "GWC"] = $ ARR;		         
		        // print_r ($ ARR); 
		        // Die (); 
		    } 

		} 

		echo "the OK"; 
		// echo '<script language="JavaScript">; 
  // Alert ( "plus commercially successful!");location.href="#";</script>;';
          
  //         exit;

 // var_dump($_SESSION);

 //    die();
		
		
	?>

  A. First of all to understand add an item to a shopping cart status There are three kinds of situations.

1. no shopping cart.

2. have a shopping cart, but add to the cart shopping cart of goods is not.

3. Have the car, add to the cart of goods also have a shopping cart, it would increase the number of values ​​in the shopping cart.

 

II. Add to Cart pages need to pass two parameters to the quantity php, id and processing of goods purchased. In front of a few lines of php it is to accept those two parameters.

 

III. To open session. seesion_start first line (); otherwise, the information is stored into the inactive session. While in the back can var_dump () out. But the display of goods in the shopping cart page or var_dump the session before.

 

IV. So how many goods deposited into session? In fact, an array of additions and deletions to change search operation session.

session is an array. It is just across page views. Deposit into the session, the ultimate goal is to be able to use it again in other pages. For example, the above code to product information saved to $ _SESSION [ 'gwc'] in, just to be able to obtain the $ _SESSION again in the shopping cart page [ 'gwc'], use the foreach loop your deposit to $ _SESSION [ 'gwc'] of commodity traverse it.

 

V. As

arr $ [ $ goods_id ] = Array ( 
		    	'goods_id' => $ goods_id,		    				 
		    	'NUM' => $ Number The 
		    				); 
This code Why is there only keep or to keep these two parameters, which is based on shopping cart page display requirements, the number is not difficult to understand, plus you buy a few pieces on display a few pieces. id in the end is why deposit into the shopping cart show business logic is, according to commodity id, then from the database
id for all product information. Namely the implementation of "select * form goods where id = product id" get

Guess you like

Origin www.cnblogs.com/mzzone/p/11026965.html