本项目基于www.webslesson.info上的例子修改而来,Bootstrap 5 加持,经过wnmp(PHP7.2)测试通过,可以做为初学者学习使用。
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
// connect to DB by mysqli
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPWD' ,'password');
define('DB' ,'test');
$connect = mysqli_connect(DBHOST, DBUSER, DBPWD, DB);
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
if(!in_array($_GET["id"], $item_array_id))
{
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
}
else
{
echo '<script>alert("商品已经添加")</script>';
echo '<script>window.location="index.php"</script>';
}
}
else
{
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["item_id"] == $_GET["id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo '<script>alert("商品已经删除")</script>';
echo '<script>window.location="index.php"</script>';
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP 购物车 项目演示</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<body>
<br />
<div class="container">
<h3 align="center">一个简单的PHP购物车项目</h3><br />
<h5>
<ul>
<li>测试平台:WNMP+PHP7.2</li>
<li>该演示只允许添加一次商品</li>
<li>未经过Ajax优化</li>
<li>无OOP部分</li>
</ul>
</h5>
<br />
<?php
$query = "SELECT * FROM tbl_product ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
?>
<div class="row module-row">
<?php
while($row = mysqli_fetch_array($result))
{
?>
<div class="col-md-4">
<div class="card">
<form method="post" action="index.php?action=add&id=<?php echo $row["id"]; ?>">
<div style="card-body" align="center">
<img src="<?php echo $row["image"]; ?>" class="card-img-top" style="height:40vh" /><br />
<h4 class="text-info"><?php echo $row["name"]; ?></h4>
<h4 class="text-danger">$ <?php echo $row["price"]; ?></h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />
<input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="添加到购物车" />
</div>
</form>
</div>
</div>
<?php
}?>
</div>
<?php
}
?>
<div style="clear:both"></div>
<br />
<h3>购物车明细</h3>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="40%">商品名称</th>
<th width="10%">数量</th>
<th width="20%">单价</th>
<th width="15%">总价</th>
<th width="5%">操作</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td><?php echo $values["item_name"]; ?></td>
<td><?php echo $values["item_quantity"]; ?></td>
<td>$ <?php echo $values["item_price"]; ?></td>
<td>$ <?php echo number_format($values["item_quantity"] * $values["item_price"], 2); ?></td>
<td><a href="index.php?action=delete&id=<?php echo $values["item_id"]; ?>">
<span class="text-danger">Remove</span>
</a>
</td>
</tr>
<?php
$total = $total + ($values["item_quantity"] * $values["item_price"]);
}
?>
<tr>
<td colspan="3" align="right">价格汇总</td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<br/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</body>
</html>
SQL代码:
CREATE DATABASE IF NOT EXISTS `test`;
--
-- Table structure for table `tbl_product`
--
CREATE TABLE IF NOT EXISTS `tbl_product` (
`id` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`price` double(10,2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `tbl_product`
--
INSERT INTO `tbl_product` (`id`, `name`, `image`, `price`) VALUES
(1, 'Samsung J2 Pro', '1.jpg', 100.00),
(2, 'HP Notebook', '2.jpg', 299.00),
(3, 'Panasonic T44 Lite', '3.jpg', 125.00);
效果如下:
(图片上传失败)