This paper mainly introduces the implementation method of PHP to realize the function of shopping cart (taking Big Apple Shopping Network as an example), which has good reference value. Let's take a look at the little series.
The first is a few simple landing pages
<body><form action= "chuli.php" method= "POST" > <p style= "margin-left:500px; margin-top:200px; height:250px; width:250px; border:1px dashed Black "> <p style=" margin-left:100px; ">
After the login page is written, you need to go to the processing page and bring up the user name and password from the database:
<?phpsession_start (); The opening session must be written to the first header ("Content-type:text/html;charset=utf-8"), $uid =$_post["UID"]; Get the username and password from the login page $pwd=$_post["pwd"];include ("DADB.class.php"); $db =new dadb (); $sql = "Select password from login where Username= ' {$uid} ', $arr = $db->query ($sql), if ($arr [0][0]== $pwd &&!empty ($PWD))//Determine the password entered is the same as the password taken, And the password cannot be empty {$_session["UID"]= $uid; Header ("location:main.php");} else{echo "Login Failed";}
This shows the login page
Below to go to the main page, from the database to get all the fruit information, and then we can add to the shopping cart this function
Main Page display diagram
Next is the Add Shopping cart page
<?phpsession_start (); $ids = $_get["IDs"];if (Empty ($_session["GWC"]) { //1. Shopping cart is empty, first click add cart $arr = Array ( array ($ids, 1) ); $_session["GWC"]= $arr;} else{ //Is not the first time to click //To determine whether the item is in the cart $arr = $_session["GWC");//Save First $chuxian = false; foreach ($arr as $v) { if ($v [0]== $ids) { $chuxian = true; } } if ($chuxian) { //3. If the item is in the cart for ($i =0; $i <count ($arr); $i + +) { if ($arr [$i][0]== $ids { $arr [$i][1]+=1; } } $_session["GWC"] = $arr; } else { //2. If the item is not in the cart $ASG = Array ($ids, 1); $arr [] = $ASG; $_session["GWC"] = $arr; }} Header ("location:gouwuche.php");
Then first the shopping owner interface, as follows
The following items are available in the
And then we get to the delete page, when the shopping cart has only one item and more than one item to do the processing
<?phpsession_start (); $sy = $_get["Sy"]; Locate the data according to the index $arr = $_session["GWC"]; $arr [$sy]; The data to be deleted//if the quantity is not 1, the quantity is reduced by 1if ($arr [$sy][1]>1) { $arr [$sy][1] = $arr [$sy][1]-1;} else//If the quantity is 1, remove { unset ($arr [$sy]);} $_session["GWC"] = $arr; Finally save the contents of the Shopping cart header ("location:gouwuche.php");
As for the submission page, we have to think of balance, inventory and other factors, so more cumbersome,
Not I afraid, on the code.
<?php session_start (); Header ("Content-type:text/html;charset=utf-8"); Prevent garbled $uid=$_session["UID"];//first check the account balance include ("DADB.class.php"); $db =new dadb (); $ysql = "Select accounts from Login Where Username= ' {$uid} ' "; $yarr = $db->query ($ysql); $yarr [0][0];//total//Shopping cart Total price, preceded by $arr=array (); if (!empty ($_ session["GWC"]) {$arr =$_session["GWC"];} $sum =0;foreach ($arr as $v) {$v The number of products in the 1];//cart $psql = "Select price from fruit WHERE ids= ' {$v [0]} '"; $parr = $db->query ($psql); foreach ($parr as $k) {$k [unit price of the 0];//product $sum + = $k [0]* $v [1]; }}//determine if the balance is satisfied with the purchase if ($yarr [0][0]>= $sum) {//Balance satisfied, to determine inventory foreach ($arr as $v) {$ksql = "select number from fruit where ids= ' {$v [0]} ' "; $karr = $db->query ($ksql); $karr [0][0];//This is an inventory if ($karr [0][0]< $v [1])//indicates insufficient inventory, at this time to give customers the inventory of insufficient {echo "inventory insufficient"; Exit }}//After judging the order must be submitted//account deducted Balance $kcsql = "Update login set account=account-{$sum} where Username= ' {$uid} '"; $db->query ($kcsql, 0);//This is the modification statement, so add 0//Subtract inventory foreach ($arr as $v) {$kcksql = "updaTe Fruit Set number=number-$v [1] where ids= ' {$v [0]} ' "; $db->query ($kcksql, 0); }//all the work is done, then we should submit the order//Here I made two tables in the database, add the submitted order to the table can be saved//Add order $DDH = Date ("Ymdhis"); $time = Date ("y-m-d h:i:s"); SDD = "INSERT into orders values (' {$DDH} ', ' {$uid} ', ' {$time} ') '; $db->query ($SDD, 0);//Add Order Details foreach ($arr as $v) {$ SDDXQ = "INSERT into OrderDetails values (' ', ' {$DDH} ', ' {$v [0]} ', ' {$v [1]} ')"; $db->query ($SDDXQ, 0); }}else{echo "Insufficient balance"; Exit;}? >
The above is the whole content of this article, I hope that everyone's study has helped.