PHP Write Shopping Cart

Source: Internet
Author: User

First write the landing page, the code is as follows:

<body>

To process the page code:
<?phpsession_start (); Include (".. /dbda.class.php "); $db = new Dbda (); $zhang = $_post[" Zhang "]; $pwd = $_post[" pwd "]; $sql =" Select Mi from Yonghu WHERE Zhang = ' {$zhang} ', $arr = $db->query ($sql), if (!empty ($zhang) &&!empty ($mi) && $mi = $arr) {    $_session ["Zhang"] = $zhang;    Header ("location:zym.php");} else{    echo "failed to log in";}? >

Next comes the main page:

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHPSession_Start(); ?> <table width= "50%" border= "1" cellpadding= "0" cellspacing= "0" > <tr> &lt ;td> code </td> <td> name </td> <td> Price </td> &LT;TD&G T; Origin </td> <td> inventory </td> <td> Operations </td> </tr>
</table> </body>

Processing code:            <?  PHP            require ". /dbda.class.php ";            $db = new Dbda ();            $sql = "SELECT * from fruit";            $arr = query ($sql)            , $db foreach ($arr as $v) {echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> & lt;td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td> <a href= ' gwc_chuli.php?ids={$v[0]} ' > Add to Cart </a></td> </tr> ';}?>  

Shopping Cart

if(Empty($_session["GWC"])) {     //if it is the first time to add a shopping cart, create a two-dimensional array to store in the session     $arr=Array(         Array($ids, 1)     ); $_session["GWC"] =$arr; } Else {     $arr=$_session["GWC"]; $bs=false;//whether it appears     foreach($arr  as $v)     {         if($v[0]==$ids)         {             $bs=true; }     }          if($bs)     {       //2. If the item is present in the cart, add 1 to the quantity         foreach($arr  as $k=$v)         {             if($v[0] = =$ids)            {                 $arr[$k][1]++;//Here we use a variable k to get the value and assignment, because just the value given to $v to represent the value of the array, in fact, the value of the array is not changed because of $v change two             }         }         $_session["GWC"] =$arr; }     Else     {        //1. If the product does not exist in the shopping cart, create a one-dimensional array and throw it into two dimensions.         $attr=Array($ids, 1); $arr[] =$attr; $_session["GWC"] =$arr; } }  Header("location:gwc_list.php");//back to the list of interfaces

View Shopping Cart

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHPSession_Start(); $arr=$_session["GWC"]; require"DataBase.class.php"; $db=Newdatebase (); $sum= 0; foreach($arr  as $k=$v) {                $v[0]; $v[1]; $sql= "SELECT * from fruit where ids= ' {$v[0]} ' "; $af=$db, Query ($sql); Echo"<tr> <td>{$v[0]}</td> <td>{$af[0][1]}</td> <td>{$af[0][2]}</td> <td>{$v[1]}</td> <td><a href= ' gwc_del.php?sy={$k} ' > Delete </a></td> </tr>"; $sum=$sum+$v[1] *$af[0] [2]; }        ? ></table><div><?PHP$gs=Count($arr);//There are several items in the shopping cartEcho"The shopping cart has a total of$gs} commodity, the total price of the product is: {$sum} meta "; ?> </div> </body>

Delete items

<?PHPSession_Start();$sy=$_get["Sy"];//if the number of fruits is greater than 1, subtract 1//if the number of fruits equals 1 removed$arr=$_session["GWC"];if($arr[$sy][1] > 1) {    //if the number of fruits is greater than 1, minus 1    $arr[$sy][1]--;} Else {    unset($arr[$sy]); $arr=array_values($arr);}$_session["GWC"] =$arr;Header("location:gwc_chakan.php");

PHP Write Shopping 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.