Speaking of shopping cart, experienced people first think of the use of session. Yes, session can effectively store user information on the client to avoid loss during the storage process, which avoids a lot of trouble.
Speaking of shopping cart, experienced people first think of the use of session. Yes, session can effectively store user information on the client to avoid loss during the storage process, which avoids a lot of trouble.
The thinkphp shopping cart implementation method is tested by myself. if you do not understand this, refer to the article about using session to implement the shopping cart function.
Class Cart extends Think {
// Name of the current shopping cart
Public $ sessionName;
// Total shopping cart price
Public $ totalPrice
Public function _ construct ($ sessionName)
{
$ This-> sessionName = $ sessionName;
If (! Isset ($ _ SESSION [$ this-> sessionName])
{
$ _ SESSION [$ this-> sessionName] = "";
}
}
// Obtain information about the shopping cart.
Public function getCart (){
$ Cur_cart_array = $ _ SESSION [$ this-> sessionName];
Return $ cur_cart_array;
}
// Obtain the shopping cart item list
Public function getCartList ()
{
$ Cur_cart_array = $ _ SESSION [$ this-> sessionName];
If ($ cur_cart_array! = "")
{
$ Mode_goods_data = M ("goods_data ");
$ Len = count ($ cur_cart_array );
For ($ I = 0; $ I <$ len; $ I ++)
{
$ Goodsid = $ cur_cart_array [$ I] ["id"];
$ Num = $ cur_cart_array [$ I] ["num"];
$ Query = "select (select sfilename from goods_pic where goodsid =. goodsid order by sno desc limit 0, 1) as sfilename, B. clsname as clsname,. goodsid as goodsid,. goodsname as goodsname,. price as Price,. storageqty as Storageqty from goods_data a left join goods_cls B on. clsid = B. clsid where. goodsid = $ goodsid ";
$ List = $ mode_goods_data-> query ($ query );
$ List [0] ["qty"] = $ num;
$ List [0] ["amount"] = $ num * $ list [0] ["Price"];
$ CartList [$ I] = $ list [0];
$ TotalPrice + = $ list [0] ["amount"];
}
// Return the total price of the product
$ This-> totalPrice = $ totalPrice;
Return $ cartList;
}
}
// Add to the shopping cart, the item id of the shopping cart, and the number of items in the shopping cart
Public function addcart ($ goods_id, $ goods_num ){
$ Cur_cart_array = $ _ SESSION [$ this-> sessionName];
If ($ cur_cart_array = "")
{
$ Cart_info [0] ["id"] = $ goods_id; // Save the item id to the array
$ Cart_info [0] ["num"] = $ goods_num; // Save the number of items to the array.
$ _ SESSION [$ this-> sessionName] = $ cart_info;
}
Else
{
// Returns the maximum value of the array key name in reverse order.
$ Ar_keys = array_keys ($ cur_cart_array );
$ Len = count ($ ar_keys );
$ Max_array_keyid = $ ar_keys [$ len-1] + 1;
// Traverse the current shopping cart array
// Traverses the 0 value of each item information array. if the key value is 0 and the keys are the same, this item has been added to the shopping cart.
$ Is_exist = $ this-> isexist ($ goods_id, $ goods_num, $ cur_cart_array );
If ($ is_exist = false)
{
$ Cur_cart_array [$ max_array_keyid] ["id"] = $ goods_id;
$ Cur_cart_array [$ max_array_keyid] ["num"] = $ goods_num;
$ _ SESSION [$ this-> sessionName] = $ cur_cart_array;
}
Else
{
$ Arr_exist = explode ("/", $ is_exist );
$ Id = $ arr_exist [0];
$ Num = $ arr_exist [1];
$ Cur_cart_array [$ id] ["num"] = $ num;
$ _ SESSION [$ this-> sessionName] = $ cur_cart_array;
}
}
}
// Determine whether the same item exists in the shopping cart
Public function isexist ($ id, $ num, $ array)
{
$ Isexist = false;
Foreach ($ array as $ key1 => $ value)
{
Foreach ($ value as $ key => $ arrayid)
{
If ($ key = "id" & $ arrayid = $ id)
{
$ Num = $ value ["num"] + $ num;
$ Isexist = $ key1. "/". $ num;
}
}
}
Return $ isexist;
}
Thinkphp development makes it easier for us to proceed.
// Delete from the shopping cart
Public function delcart ($ goods_array_id ){
// Returns the serialized array.
$ Cur_goods_array =$ _ SESSION [$ this-> sessionName];
// Delete the position of the item in the array
Unset ($ cur_goods_array [$ goods_array_id]);
$ _ SESSION [$ this-> sessionName] = $ cur_cart_array;
// Serialize the array and save it to the cookie.
}
// Clear the shopping cart
Public function emptycart (){
$ _ SESSION [$ this-> sessionName] = "";
}
// Modify the quantity of items in the shopping cart
Public function update_cart ($ up_id, $ up_num ){
// Returns the serialized array.
$ Cur_goods_array =$ _ SESSION [$ this-> sessionName];
$ Cur_goods_array [$ up_id] ["num"] = $ up_num;
$ _ SESSION [$ this-> sessionName] = $ cur_cart_array;
}
}
?>