Simple PHP shopping cart code

Time:2020-8-4

This paper introduces a PHP shopping cart code, complete function implementation, has a certain reference value

Here we provide you with a simple PHP shopping cart code, from the increase of shopping products and the occurrence of purchase, in the mall development, this function is indispensable

The specific analysis is as follows:

The operation of goods in the shopping cart is as follows: add goods, delete goods, and submit orders;

The essence of the method is: store the session into the array, and add, delete and modify the array. Each group of records in the array is the information of a commodity (number, price, etc.);

The idea of shopping cart is to record a two-dimensional array with session. One dimension represents each commodity, and two-dimensional contains the ID of the commodity, the quantity of the commodity, etc., which can be increased by itself. In any case, it is two-dimensional. You can add as many commodity attributes as you like.

The operation process of shopping cart: first, log in to the website to browse the goods; then, purchase the specified goods and enter the shopping cart page, where you can change the quantity of goods, delete the goods, empty the shopping cart, continue shopping, etc.; finally, generate and submit orders.

See the powerful notes for details;

Log in first;

<body>
<h1>Login page</h1>
<form action="dengrucl.php" method="post">
  < div > account number: < input type = "text" name = "Zhang" / > < / div >
  < div > password: < input type = "text" name = "Mi" / > < / div > password
  < input type = "submit" value = "login" / >
</form>

</body>

Figure:

Login processing:

<?php
session_start();
include ("db.class.php");
$db = new db();
$zhang = $_POST["zhang"];
$mi = $_POST["mi"];
$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 "login failed";
}

?>

A very simple login, nothing to say;

After logging in, he went directly to the main page:

<body>
<h1>Changjiaoxuan shopping mall</h1>
<table border="1" cellpadding="0" cellspacing="0" width="100%" >
  <tr>
    <td>Code name</td>
    <td>Name of fruit</td>
    <td>Fruit price</td>
    <td>Fruit producing area</td>
    <td>Fruit inventory</td>
    <td>Operation</td>
  </tr>
  <?php
  session_start();
$5 Session ["Zhang"] = Xiaoming "; // without logging in, save the session directly
  include ("db.class.php");
  $db = new db();
  $sql = "select * from sgbiao";
  $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=' zymcl.php?ids= {$v [0]} > > add to cart</a>
    
</td>
  </tr>";
  }

  ?>
  <?php
  //This shows how many products there are in the shopping cart, and the total price of the products
  $ann=array();
  if(!empty($_SESSION["gwc"]))
  {
    $ann=$_SESSION["gwc"];

  }
  $zhonglei = count($ann);

  $aa=0;
  foreach($ann as $k)
  {

    $k [0]; // fruit code
    $k [1]; // number of fruits
    $sql1="select jiage from sgbiao where ids='{$k[0]}'";

    $danjia=$db->Query($sql1);

    foreach($danjia as $n)
    {

      $aa=$aa + $n[0]*$k[1];
    }


  }
  Echo "quantity: {$Zhonglei} < br / >
Price: < mark > {$AA} ";
  ?>
</table>

<a href=" tijiao.php "Rel =" external nofollow "rel =" external nofollow "> view account</a>
<a href=" ckgwc.php "Rel =" external nofollow "> View shopping cart</a>

</body>

Photo:

When I click Add to Cart:

The quantity and price above have changed, indicating that they have been added to the shopping cart;

Take a look at how it’s handled (powerful comments)

<?php
session_start();
//
$ids = $_GET["ids"];
if(empty($_SESSION["gwc"]))
{
  //If the cart you click on is empty (first added)

  //If the shopping cart is empty, make a two-dimensional array,
  $arr = array(
    array($ids,1)
    //One dimensional array, take IDS, click the first time to add one
  );
  $_SESSION["gwc"]=$arr;
  //Throw it into the session
}
else
  //It's not the first time you click here
{
  //First determine whether the product is already in the shopping cart, and use $IDs
  $arr = $_SESSION["gwc"];
  //Take out the status of the shopping cart

  $chuxian = false;
//Define a variable; it is used to indicate whether it appears or not, and it is not by default
  foreach ($arr as $v) {
    //Facilitate him
    //If you have this product in it
    If ($v [0] = = $IDS) // if the retrieved $v [0] (product code) is equal to $IDS, then it proves that there is a product in the shopping cart
    {
      $chuxian = true;
      //If it appears, change Chuxian to true

    }
  }
  if($chuxian)
  {
    //This item is in the shopping cart
    for($i=0;$i<count($arr);$i++)
    {
      if($arr[$i][0] == $ids)
      {
        //Add 1 to the item number
        $arr[$i][1] += 1;
      }
    }
    $_SESSION["gwc"] = $arr;

  }
    else
      {
        //Here's all that's left: there's something in the cart, but there's no product
        $asg = array($ids,1);
        //Let's set a small array
        $arr[] = $asg;
        $_SESSION["gwc"]=$arr;
      }

}
header("location:zym.php")


?>

Next, let’s do the shopping cart page:

<body>
<h1>View shopping cart</h1>
<table width="100%" border="1"cellspacing="0" cellpadding="0">
  <tr>
    <td>Trade name</td>
    <td>Unit price of goods</td>
    <td>Quantity of goods</td>
    <td>Operation</td>
  </tr>

  <?php
  session_start();
  if(!empty($_SESSION["gwc"]))
  {
    $arr = array();
    $arr = $_SESSION["gwc"];
    //Making arrays
  }
  include ('db.class.php');
  $db = new db();
  foreach ($arr as $v)
  {
    global $db;
    $sql = "select * from sgbiao WHERE ids = '{$v[0]}'";
    $att = $db->query($sql);
    foreach ($att as $a)
    {
      echo "<tr>
    <td>{$a[1]}</td>
    <td>{$a[2]}</td>
    <td>{$v[1]}</td>
    <td><a href=' shanchu.php?ids= {$a [0]}] delete < / a > from</td>
  </tr> ";
//Names of fruits and vegetables
//Unit price
//Take int quantity
//This place can also be indexed shanchu.php?sy= {$v}
    }
  }
  ?>

</table>

<a href=" tijiao.php "Rel = external nofollow" rel = external nofollow "> submit order</a>
</body>

Above:

You can see that the number of big apples is 4. If I click delete, the condition is that there are big apples and the number is greater than one. Click Delete to reduce the number by one

The number of white grapes is 1. If I click delete, the condition is that the number is not greater than one, so that the array is removed;

With that said, take a look at the delete page:

<?php
session_start();
$ids = $_GET["ids"];
$arr = $_SESSION["gwc"];
//var_dump($arr);
//Take index 2 (quantity)
foreach ($arr as $key=>$v)
{
  if($v[0]==$ids)
  {
    if($v[1]>1){
      //Data to delete
      $arr[$key][1]-=1;
    }
    else{
      //If the number is 1, remove the array
      unset($arr[$key]);
    }
  }

}

$_SESSION["gwc"] = $arr;
//Remember to throw it into the session
header("location:ckgwc.php");
//Delete and jump back

High energy!!

To submit an order, I’ll just come to a processing page. If you need to fill in the link, you can:

<?php
session_start();
include ("db.class.php");
$db = new db();

//Whether the balance used is satisfied
$zhang = $_SESSION["zhang"];
//Get user name
$sye = "select zhanghu from yonghu WHERE zhang = '{$zhang}'";
$ye = $db->query($sye);
$ye [0] [0]; // this is the balance

$ann=array();
if(!empty($_SESSION["gwc"]))
{
  $ann=$_SESSION["gwc"];

}
$zhonglei = count($ann);

$AA = 0; // total price
foreach($ann as $k)
{

  $k [0]; // fruit code
  $k [1]; // number of fruits
  $sql1="select jiage from sgbiao where ids='{$k[0]}'";

  $danjia=$db->Query($sql1);

  foreach($danjia as $n)
  {

    $aa=$aa + $n[0]*$k[1];
  }


}
//Judge whether the balance is satisfied
if($ye[0][0]>=$aa)
{
  //The money is enough, judge the stock

  foreach($ann as $v)
  {
    $skc = "select sgname,kucun from sgbiao WHERE ids='{$v[0]}'";
    //Fruit code $v [0]
    $akc = $db->query($skc);
    $AKC [0] [1]; // inventory
    //Compare whether the inventory is satisfied
    if($akc[0][1]<$v[1])
    {
      Echo "{$AKC [0] [0]} insufficient inventory";
      //Exit
      exit;
    }

  }
//Order submission:
//i. The total price of this purchase is deducted from the user's account
//II. Deduct the purchase quantity of each commodity from the inventory
//III. add the information of this purchase to the order form and order content table
  //Deduct account balance
$skcye = "update yonghu set zhanghu = zhanghu-{$aa} WHERE zhang = '{$zhang}'";
  $db->query($skcye,0);
  //Deduct inventory
  foreach($ann as $v)
  {
    $skckc = "update sgbiao set kucun = kucun-{$v[1]} WHERE ids='{$v[0]}'";
    //Fruit code $v [0]
    $db->query($skckc,0);
  }
  //Add order information
  //Take current time
  $time = time();
  //Auto generate order number
  $ddh = date("YmdHis");
  $sdd = "insert into dingdan VALUES ('{$ddh}','$zhang','$time')";
  $db->query($sdd,0);
  //Add order content
  foreach ($ann as $v)
  {
    $sddxq = "insert into ddneirong VALUES ('','$ddh','{$v[0]}','{$v[1]}')";
    $db->query($sddxq,0);
  }


}
else
{
  Echo "money is not enough";
  exit;
}

In this way, the functions of shopping cart have been realized in general;

Take a look at the effect of clicking submit order:

1. Reduce fruit inventory:

2. Add order:

3. Add order content:

4. Deduct the balance of the buyer’s account:

That’s the shopping cart

The above is a simple PHP shopping cart code details, more information about PHP shopping cart, please pay attention to other related articles in developeppaer!