Php shopping cart project_php tutorial

Source: Internet
Author: User
Php shopping cart program. This is a simple php code developed and used by myself for shopping cart functions. a few files are used and no database is used to implement shopping cart. if the user closes the browser, the shopping cart vendor is a simple php code developed and used for shopping cart functions. it uses a few files and does not use databases to implement the shopping cart. if the user closes the browser, all the items in the shopping cart will be lost. if you need it, you can improve it. it is better to use database + session + cookie.
The code is as follows:

Class Shopcar
{
// Product list
Public $ productList = array ();

/**
*
* @ Param unknown_type $ the product to be imported
* @ Return true no such item exists in the shopping cart
*/
Public function checkProduct ($ product)
{

For ($ I = 0; $ I ProductList); $ I ++)
{

If ($ this-> productList [$ I] ['name'] = $ product ['name'])
Return $ I;
}

Return-1;

}
// Add to shopping cart
Public function add ($ product)
{
$ I = $ this-> checkProduct ($ product );
If ($ I =-1)
Array_push ($ this-> productList, $ product );
Else
$ This-> productList [$ I] ['num'] + = $ product ['num'];
}
// Delete
Public function delete ($ product)
{
$ I = $ this-> checkProduct ($ product );
If ($ I! =-1)
Array_splice ($ this-> productList, $ I, 1 );

}

// Returns information about all products.
Public function show ()
{
Return $ this-> productList;

}


}

Html





Insert title here

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.