Code of a simple php + Ajax Shopping Cart program (1/2)

Source: Internet
Author: User

This article will recommend a good shopping cart effect. The main requirements here include a few things. One is written in php for the shopping cart class, and the other is jquery for Ajax operations, there is also a jquery plug-in thickbox. Let's take a look.

Shopping cart type: shop_cart.php
Shopping Cart operation: cart_action.php
Home: index.html
Jquery is used for Ajax operations, and a jquery plug-in thickbox

Let's not talk about it anymore. You can first look at the effect example.
Shop_cart.php is of course the core of the shopping cart, but this class is very simple, because he introduced cart_action.php for external operations. So this class seems quite streamlined.
Shopping Cart class shop_cart.php

The Code is as follows: Copy code

Cart_name = $ name;
$ This-> items =$ _ SESSION [$ this-> cart_name];
}

/**
* SetItemQuantity ()-Set the quantity of an item.
*
* @ Param string $ order_code The order code of the item.
* @ Param int $ quantity The quantity.
*/
Function setItemQuantity ($ order_code, $ quantity ){
$ This-> items [$ order_code] = $ quantity;
}

/**
* GetItemPrice ()-Get the price of an item.
*
* @ Param string $ order_code The order code of the item.
* @ Return int The price.
*/
Function getItemPrice ($ order_code ){
// This is where the code taht retrieves prices
// Goes. We'll just say everything costs $9.99 for this tutorial.
Return 9.99;
}

/**
* GetItemName ()-Get the name of an item.
*
* @ Param string $ order_code The order code of the item.
*/
Function getItemName ($ order_code ){
// This is where the code that retrieves product names
// Goes. We'll just return something generic for this tutorial.
Return 'my Product ('. $ order_code .')';
}

/**
* GetItems ()-Get all items.
*
* @ Return array The items.
*/
Function getItems (){
Return $ this-> items;
}

/**
* HasItems ()-Checks to see if there are items in the cart.
*
* @ Return bool True if there are items.
*/
Function hasItems (){
Return (bool) $ this-> items;
}

/**
* GetItemQuantity ()-Get the quantity of an item in the cart.
*
* @ Param string $ order_code The order code.
* @ Return int The quantity.
*/
Function getItemQuantity ($ order_code ){
Return (int) $ this-> items [$ order_code];
}

/**
* Clean ()-Cleanup the cart contents. If any items have
* Quantity less than one, remove them.
*/
Function clean (){
Foreach ($ this-> items as $ order_code => $ quantity ){
If ($ quantity <1) unset ($ this-> items [$ order_code]);
}
}

/**
* Save ()-Saves the cart to a session variable.
*/
Function save (){
$ This-> clean ();
$ _ SESSION [$ this-> cart_name] = $ this-> items;
}
}

?>

For cart_action, it implements the intermediate role of the shop_cart class and index for updating, deleting, and adding commodity operations.
Cart_action.php

The Code is as follows: Copy code

GetItemQuantity ($ _ GET ['order _ Code']) + $ _ GET ['quantity '];
$ Cart-> setItemQuantity ($ _ GET ['order _ Code'], $ quantity );
} Else {

If (! Empty ($ _ GET ['quantity ']) {
Foreach ($ _ GET ['quantity '] as $ order_code => $ quantity ){
$ Cart-> setItemQuantity ($ order_code, $ quantity );
}
}

If (! Empty ($ _ GET ['delete']) {
Foreach ($ _ GET ['delete'] as $ order_code ){
$ Cart-> setItemQuantity ($ order_code, 0 );
}
}
}
$ Cart-> save ();

Header ('location: cart. php ');

?>

You can also perform external operations by using javasindex.html, that is, adding operations.

The Code is as follows: Copy code

<! 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 = UTF-8">
<Title> Shopping Cart </title>
<Script src = "js/jquery-1.2.6.pack.js" type = "text/javascript"> </script>
<Script src = "js/jquery. color. js" type = "text/javascript"> </script>
<Script src = "js/thickbox. js" type = "text/javascript"> </script>
<Script src = "js/cart. js" type = "text/javascript"> </script>
<Link href = "css/style.css" rel = "stylesheet" type = "text/css" media = "screen"/>
<Link href = "css/thickbox.css" rel = "stylesheet" type = "text/css" media = "screen"/>

<Script type = "text/javascript">
$ (Function (){
$ ("Form. cart_form"). submit (function (){
Var title = "Your Shopping Cart ";
Var orderCode = $ ("input [name = order_code]", this). val ();
Var quantity = $ ("input [name = quantity]", this). val ();
Var url = "cart_action.php? Order_code = "+ orderCode +" & amp; quantity = "+ quantity +" & amp; amp; TB_iframe = true & amp; height = 400 & amp; width = 780 ";
Tb_show (title, url, false );

Return false;
});
});
</Script>
</Head>
<Body>
<Div id = "container">
<H1> shopping cart <A href = "cart. php? KeepThis = true & amp; TB_iframe = true & amp; height = 400 & amp; width = 780 "title =" Your Shopping Cart "class =" thickbox "> open the Shopping Cart </a>
<Hr/>
<A href = "cart_action.php? Order_code = KWL-JFE & amp; quantity = 3 & amp; TB_iframe = true & amp; height = 400 & amp; width = 780 "title =" Your Shopping Cart "class =" thickbox "> Add three KWL-JFE to the Cart </a>
<Hr/>
<Form class = "cart_form" action = "cart_action.php" method = "get">
<Input type = "hidden" name = "order_code" value = "KWL-JFE"/>
<Label> KWL-JFE: <input class = "center" type = "text" name = "quantity" value = "1" size = "3"?> </Label>
<Input type = "submit" name = "submit" value = "add to shopping cart"/>
</Form>
</Div>
</Body>
</Html>

And cart. php. This is our shopping cart.

The Code is as follows: Copy code
<? Php
Include ('shopping _ cart. class. php ');
Session_start ();
$ Cart = new Shopping_Cart ('shopping _ cart ');
?>
<! 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">
<META HTTP-EQUIV = "Content-Type" CONTENT = "text/html; charset = UTF-8">
<Head>
<Title> Shopping Cart </title>
<Script src = "js/jquery-1.2.6.pack.js" type = "text/javascript"> </script>
<Script src = "js/jquery. color. js" type = "text/javascript"> </script>
<Script src = "js/cart. js" type = "text/javascript"> </script>
<Link href = "css/cart.css" rel = "stylesheet" type = "text/css" media = "screen"/>
</Head>
<Body>
<Div id = "container">
<H1> Shopping Cart <? Php if ($ Cart-> hasItems ():?>
<Form action = "cart_action.php" method = "get">
<Table id = "cart">
<Tr>
<Th> quantity </th>
<Th> product name </th>
<Th> item No. </th>
<Th> unit price </th>
<Th> total price </th>
<Th> Delete </th>
</Tr>
<? Php
$ Total_price = $ I = 0;
Foreach ($ Cart-> getItems () as $ order_code => $ quantity ):
$ Total_price + = $ quantity * $ Cart-> getItemPrice ($ order_code );
?>
<? Php echo $ I ++ % 2 = 0? "<Tr>": "<tr class = 'odd'>";?>
<Td class = "quantity center"> <input type = "text" name = "quantity [<? Php echo $ order_code;?>] "Size =" 3 "value =" <? Php echo $ quantity;?> "Tabindex =" <? Php echo $ I;?> "/> </Td>
<Td class = "item_name"> <? Php echo $ Cart-> getItemName ($ order_code);?> </Td>
<Td class = "order_code"> <? Php echo $ order_code;?> </Td>
<Td class = "unit_price" >$ <? Php echo $ Cart-> getItemPrice ($ order_code);?> </Td>
<Td class = "extended_price" >$ <? Php echo ($ Cart-> getItemPrice ($ order_code) * $ quantity);?> </Td>
<Td class = "remove center"> <input type = "checkbox" name = "remove []" value = "<? Php echo $ order_code;?> "/> </Td>
</Tr>
<? Php endforeach;?>
<Tr> <td colspan = "2"> </td> <td colspan = "3" id = "total_price"> your total consumption amount is: ¥ <? Php echo $ total_price;?> </Td> </tr>
</Table>
<Input type = "submit" name = "update" value = "save shopping cart"/>
</Form>
<? Php else:?>
<P class = "center"> you have not purchased. </p>
<? Php endif;?>
<P> <a href = "load. php"> load a simple Shopping Cart </a> </p>
</Div>
</Body>
</Html>

1 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.