Practical php shopping cart program

Source: Internet
Author: User
Tags foreach constructor php tutorial prev

Practical php Tutorial shopping cart program
I used to have a good experience before, but it also feels good to read it, so I would like to introduce it to a friend who needs it for reference.

<? Php
// Call an instance
Require_once 'cart. class. Php ';
Session_start ();
If (! Isset ($ _ SESSION ['cart']) {
$ _ SESSION ['cart'] = new cart;
}
$ Cart = & $ _ SESSION ['cart'];

If ($ _ SERVER ['request _ method'] = "POST") & ($ _ POST ['action'] = 'ADD ')){
$ P = $ _ POST ['P'];
$ Items = $ cart-> add ($ p );
}
If ($ _ GET ['action'] = 'delete') & ($ _ GET ['key']! = "")){
$ Items = $ cart-> remove ($ _ GET ['key']);
}

If ($ _ SERVER ['request _ method'] = "POST") & ($ _ POST ['action'] = 'Modi ')){
$ Key = $ _ POST ['key'];
$ Value = $ _ POST ['value'];
For ($ I = 0; $ I <count ($ key); $ I ){
$ Items = $ cart-> modi ($ key [$ I], $ value [$ I]);
 }
}

$ Items = $ cart-> getCart ();
// Print
Echo "<table border = 1> ";
Setlocale (LC_MONETARY, 'It _ it ');
Foreach ($ items as $ item ){
Echo "<tr> <form method =" post "action =" tmp. php "> ";
Echo "<td> ID :". $ item ['id']. "<input type = hidden name = key [] value = ". $ item ['id']. "> ";
Echo "<td> product:". $ item ['name'];
Echo "<td> unit price:". $ item ['price'];
Echo "<td> <input type = text name = value [] value =". $ item ['count']. "> ";
$ Sum = $ item ['count'] * $ item ['price'];
Echo "<td> Total:". round ($ sum, 2 );
Echo "<td> <input type = button value = 'delete' onclick =" location = '? Action = remove & key = ". $ item ['id']." '"> ";
}
Echo "<input type = hidden name = action value = modi> ";
Echo "<tr> <td colspan = 7> <input type = submit/> ";
Echo "</td> </form> </tr> </table> ";


?>
<Hr>
<Form method = "post" action = "tmp. php">
ID: <input type = "text" name = "p []"/>
Product name: <input type = "text" name = "p []"/>
Unit Price: <input type = "text" name = "p []"/>
Quantity: <input type = "text" name = "p []"/>
<Input type = hidden name = action value = add>
<Input type = "submit"/>
</Form>

 

<?
/**
* Cart
 *
* Shopping cart type
 *
* @ Author doodoo <pWtitle@yahoo.com.cn>
* @ Package Cart
* @ Category Cart
* @ License PHP License
* @ Access public
* @ Version $ Revision: 1.10 $
*/
Class Cart {

Var $ cart;
Var $ totalCount; // total number of items
Var $ totalPrices; // total item amount

/**
* Cart Constructor
     *
* Class constructor to ensure stable initialization of the shopping cart
     *
* @ Static
* @ Access public
* @ Return void: no return value
* @ Param void no parameter
*/
Function Cart (){
$ This-> totalCount = 0;
$ This-> totalPrice = 0;
$ This-> cart = array ();
 }
 
//}}}
// {Add ($ item)

/**
* Add items to the current shopping cart
 *
* @ Access public
* @ Param array $ item product information (one-dimensional array: array (product ID, product name, product unit price, product quantity ))
* @ Return array returns the array of items in the current shopping cart.
*/
Function add ($ item ){
If (! Is_array ($ item) | is_null ($ item) return $ this-> cart;
If (! Is_numeric (end ($ item) | (! Is_numeric (prev ($ item )))){
Echo "the price and quantity must be numbers ";
Return $ this-> cart;
  }
Reset ($ item); // This sentence is required, because the above judgment has moved the index of the array
$ Key = current ($ item );
If ($ key = "") return $ this-> cart;
If ($ this-> _ isExists ($ key) {// is the product already exists?
$ This-> cart [$ key] ['count'] = end ($ item );
Return $ this-> cart;
  }

$ This-> cart [$ key] ['id'] = $ key;
$ This-> cart [$ key] ['name'] = next ($ item );
$ This-> cart [$ key] ['price'] = next ($ item );
$ This-> cart [$ key] ['count'] = next ($ item );

Return $ this-> cart;
 }

//}}}
// {Add ($ item)

/**
* Retrieve some or all items from the current shopping cart
* When $ key = "", clear the current shopping cart
* When $ key! = "" & $ Count = "", select all items whose ID number is $ key from the current shopping cart.
* When $ key! = "" & $ Count! = "", Select $ count items with the ID number of $ key from the current shopping cart.
 *
* @ Access public
* @ Param string $ key product ID
* @ Return mixed returns an array of true and false items or items in the current shopping cart
*/
Function remove ($ key = "", $ count = ""){
If ($ key = ""){
$ This-> cart = array ();
Return true;
  }
If (! Array_key_exists ($ key, $ this-> cart) return false;
If ($ count = "") {// remove this item
Unset ($ this-> cart [$ key]);
} Else {// remove $ count items
$ This-> cart [$ key] ['count']-= $ count;
If ($ this-> cart [$ key] ['count'] <= 0) unset ($ this-> cart [$ key]);
  }
Return $ this-> cart;
 }

//}}}
// {Modi ($ key, $ value)

/**
* Change the number of items in the shopping cart with the product ID as $ key to $ value.
 *
* @ Access public
* @ Param string $ key product ID
* @ Param int $ value number of items
* @ Return array returns the array of items in the current shopping cart;
*/
Function modi ($ key, $ value ){
If (! $ This-> _ isExists ($ key) return $ this-> cart (); // this item does not exist and is returned directly
If ($ value <= 0) {// value is too small, delete all
Unset ($ this-> cart [$ key]);
Return $ this-> cart;
  }
$ This-> cart [$ key] ['count'] = $ value;
Return $ this-> cart;
 }


/**
* Returns an array of items in the current shopping cart.
 *
* @ Access public
* @ Return array returns the array of items in the current shopping cart;
*/
Function getCart (){
Return $ this-> cart;
 }

//}}}
// {_ IsExists ($ key)

/**
* Check whether there is a product with the ID of $ key in the current shopping cart.
 *
* @ Access private
* @ Param string $ key product ID
* @ Return bool true or false;
*/
Function _ isExists ($ key)
    {
If (isset ($ this-> cart [$ key]) &! Empty ($ this-> cart [$ key]) & array_key_exists ($ key, $ this-> cart ))
Return true;
Return false;
    }

//}}}
// {IsEmpty ()

/**
* Determines whether the current shopping cart is empty, that is, there is no product
 *
* @ Access public
* @ Return bool true or false;
*/
Function isEmpty (){
Return! Count ($ this-> cart );
 }

//}}}
// {_ Stat ()

/**
* Obtain partial statistics
 *
* @ Access private
* @ Return bool true or false;
*/
Function _ stat (){
If ($ this-> isEmpty () return false;
Foreach ($ this-> cart as $ item ){
$ This-> totalCount = @ end ($ item );
$ This-> totalPrices = @ prev ($ item );
  }
Return true;
 }

//}}}
// {TotalPrices ()

/**
* Get the total amount of all items in the current shopping cart
 *
* @ Access public
* @ Return float: The Returned amount;
*/
Function totalPrices (){
If ($ this-> _ stat ())
Return $ this-> totalPrices;
Return 0;
 }

//}}}
// {IsEmpty ()

/**
* Obtain the total quantity and total number of all items in the current shopping cart.
 *
* @ Access public
* @ Return int;
*/
Function totalCount (){
If ($ this-> _ stat ())
Return $ this-> totalCount;
Return 0;
 }


} // End Class Cart
?>

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.