Add a shopping cart in php and a shopping cart in php

Source: Internet
Author: User

Add a shopping cart in php and a shopping cart in php

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>

<Body>

<H1> item list
<Table width = "100%" border = "1" cellpadding = "0" cellspacing = "0">
<Tr>
<Td> fruit Code </td>
<Td> fruit name </td>
<Td> fruit price </td>
<Td> fruit origin </td>
<Td> fruit stock </td>
<Td> operation </td>
</Tr>

<? Php
Session_start ();
Include ("../fengzhuang/DBDA. class. php ");
$ Db = new DBDA ();

$ SQL = "select * from fruit ";
$ Arr = $ db-> Query ($ SQL );
Foreach ($ arr as $ v)
{
Echo "<tr>
<Td >{$ v [0]} </td>
<Td >{$ v [1]} </td>
<Td >{$ v [2]} </td>
<Td >{$ v [3]} </td>
<Td >{$ v [4]} </td>
<Td> <a href = 'add. php? Ids = {$ v [0]} '> Add to shopping cart </a> </td>
</Tr> ";
}
?>

</Table>

<? Php
Var_dump ($ _ SESSION ["gwc"]);
?>


</Body>
</Html>




<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>

<Body>
<H1> shopping cart details
<Table width = "100%" border = "1" cellpadding = "0" cellspacing = "0">
<Tr>
<Td> fruit Code </td>
<Td> quantity bought </td>
<Td> operation </td>
</Tr>

<? Php
Session_start ();

$ Arr = array ();
If (! Empty ($ _ SESSION ["gwc"])
{
$ Arr = $ _ SESSION ["gwc"];
}

Foreach ($ arr as $ v)
{
Echo "<tr>
<Td >{$ v [0]} </td>
<Td >{$ v [1]} </td>
<Td> </td>
</Tr> ";
}
?>

</Table>

</Body>
</Html>




<? Php
Session_start ();

$ Ids = $ _ GET ["ids"];

/* $ Arr = array (
Array ("k001", 10 ),
Array ("k002", 2 ),
Array ("k003", 5)
);*/

// 1. Click Add shopping cart for the first time
// 2. If this item is not found in the shopping cart
// 3. If this item exists in the shopping cart

If (empty ($ _ SESSION ["gwc"])
{
// 1. Click Add shopping cart for the first time
$ Arr = array (
Array ($ ids, 1)
);
$ _ SESSION ["gwc"] = $ arr;
}
Else
{
// Not the first click
// Determine whether the product exists in the shopping cart
$ Arr = $ _ SESSION ["gwc"];

$ Chuxian = false;
Foreach ($ arr as $ v)
{
If ($ v [0] = $ ids)
{
$ Chuxian = true;
}
}

If ($ chuxian)
{
// 3. If this item exists in the shopping cart
/* Foreach ($ arr as $ v)
{
If ($ v [0] = $ ids)
{
$ V [1] = $ v [1] + 1;
}
}*/

For ($ I = 0; $ I <count ($ arr); $ I ++)
{
If ($ arr [$ I] [0] = $ ids)
{
$ Arr [$ I] [1] + = 1;
}
}

$ _ SESSION ["gwc"] = $ arr;
}
Else
{
// 2. If this item is not found in the shopping cart
$ Asg = array ($ ids, 1 );
$ Arr [] = $ asg;
$ _ SESSION ["gwc"] = $ arr;
}

}

Header ("location: main. php ");

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.