PHP develops a simple shopping cart to implement purchase function
buy.php This page completes the purchase function
The main purpose here is to process the purchase of goods in the session, and combine the product information in the session with the purchased product information Compared.
If it is the first time to purchase an item, add the product information to the shopping cart and calculate the total price.
If you click to purchase again, the quantity of purchased items will be increased by 1, and the total price will be calculated. To recalculate, view the shopping cart link to the shopping cart page.
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>購買頁</title> </head> <body> <?php //開啟session session_start(); //獲取傳過來的商品名和價(jià)格 $name = $_GET['name']; $price = $_GET['price']; //把session中的商品信息和傳過來的(剛買的)商品信息對(duì)比 $goods = $_SESSION['goods']; if ($name == $goods[$name]['name']) { //買過的話,則總價(jià)格增加,相應(yīng)商品數(shù)量增加 $_SESSION['totalPrice'] += $price; $goods[$name]['number'] += 1; } else { //第一次買的話,將相應(yīng)的商品信息添加到session中 $goods[$name]['name'] = $name; $goods[$name]['price'] = $price; $goods[$name]['number'] += 1; $_SESSION['totalPrice'] += $price; } $_SESSION['goods'] = $goods; //購買處理完畢后跳轉(zhuǎn)到商品列表 header('location: list.php'); ?> </body> </html>