<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<body>
<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> Operations </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 Cart </a></td>
</tr> ";
}
?>
</table>
<?php
Var_dump ($_session["GWC"]);
?>
</body>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<body>
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Fruit Code </td>
<td> Purchase Quantity </td>
<td> Operations </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>
<?php
Session_Start ();
$ids = $_get["IDs"];
/* $arr = Array (
Array ("k001", 10),
Array ("k002", 2),
Array ("k003", 5)
);*/
1. First click Add Cart
2. If the item is not in the shopping cart
3. If the item is in the shopping cart
if (Empty ($_session["GWC"]))
{
1. First click Add Cart
$arr = Array (
Array ($ids, 1)
);
$_session["GWC"]= $arr;
}
Else
{
Not the first time you click
Determine if the item exists in the shopping cart
$arr = $_session["GWC"];
$chuxian = false;
foreach ($arr as $v)
{
if ($v [0]== $ids)
{
$chuxian = true;
}
}
if ($chuxian)
{
3. If the item is 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 the item is not in the shopping cart
$ASG = Array ($ids, 1);
$arr [] = $ASG;
$_session["GWC"] = $arr;
}
}
Header ("location:main.php");
Add a shopping cart to PHP