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 ");