PHPMySQL Shopping Cart program instance-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? If (! $ Session & amp ;! $ Scid) {$ sessionmd5 (uniqid (rand (); SetCookie (scid, $ session, time () 14400);} * lastnumberisexpirationtimeinseconds, 14400sec4hrs * script ec (2); script

If (! $ Session &&! $ Scid ){
$ Session = md5 (uniqid (rand ()));
SetCookie ("scid", "$ session", time () 14400 );
}/* Last number is expiration time in seconds, 14400 sec = 4 hrs */
Class Cart {
Function check_item ($ table, $ session, $ product ){
$ Query = "SELECT * FROM $ table WHERE session = '$ session' AND product =' $ product '";
$ Result = mysql_query ($ query );
If (! $ Result ){
Return 0;
}
$ NumRows = mysql_num_rows ($ result );
If ($ numRows = 0 ){
Return 0;
} Else {
$ Row = mysql_fetch_object ($ result );
Return $ row-> quantity;
}
}
Function add_item ($ table, $ session, $ product, $ quantity ){
$ Qty = $ this-> check_item ($ table, $ session, $ product );
If ($ qty = 0 ){
$ Query = "insert into $ table (session, product, quantity) VALUES ";
$ Query. = "('$ session', '$ product',' $ quantity ')";
Mysql_query ($ query );
} Else {
$ Quantity = $ qty;
$ Query = "UPDATE $ table SET quantity = '$ quantity' WHERE session = '$ session' AND ";
$ Query. = "product = '$ product '";
Mysql_query ($ query );
}
}
Function delete_item ($ table, $ session, $ product ){
$ Query = "delete from $ table WHERE session = '$ session' AND product =' $ product '";
Mysql_query ($ query );
}
Function modify_quantity ($ table, $ session, $ product, $ quantity ){
$ Query = "UPDATE $ table SET quantity = '$ quantity' WHERE session = '$ session '";
$ Query. = "AND product = '$ product '";
Mysql_query ($ query );
}
Function clear_cart ($ table, $ session ){
$ Query = "delete from $ table WHERE session = '$ session '";
Mysql_query ($ query );
}
Function cart_total ($ table, $ session ){
$ Query = "SELECT * FROM $ table WHERE session = '$ session '";
$ Result = mysql_query ($ query );
If (mysql_num_rows ($ result)> 0 ){
While ($ row = mysql_fetch_object ($ result )){
$ Query = "SELECT price FROM inventory WHERE product = '$ row-> product '";

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.