Add a shopping cart to PHP

Source: Internet
Author: User

<! 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

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.