PHP implementation of the shopping cart function, the Big Apple Shopping Network, for example, the above figure on the code ....

Source: Internet
Author: User
Tags psql

The first is a few simple landing pages

<body><form action= "chuli.php" method= "POST" >    <div style= "margin-left:500px; margin-top:200px     ; height:250px; width:250px; border:1px dashed Black ">        <div 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

<a href= ' shanchu.php?sy={$k} ' > Delete </a></td>
</tr> ";} }?> </table> <div> <a href= "gouwuche.php" > View shopping cart </a> <a href= "main.php" > Browse Products </a > <a href= "zhanghu.php" > View account </a> </div> </body>

  

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 whe        Re 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);//Here 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;}? >

  

It's no problem to implement the function

PHP implementation of the shopping cart function, the Big Apple Shopping Network, for example, on the code ....

Related Article

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.