显示带有每个项目的删除按钮的项目的购物车
cart that show item with remove button for each of them
我想用简单的 php 和 mysql 代码创建购物车,这里是查看购物车的代码,我想在 table 中查看商品,并带有删除每个按钮
在这里我创建了一个购物车并且看到了我想要一个简单的购物车所以任何人都可以告诉我如何在每个项目上显示一个删除按钮并在购物车中显示该项目
<?php
session_start();
include_once("config.php");
if(empty($_SESSION['cart']))
$_SESSION['cart'] = array();
if(isset($_POST['product_code'])){
$product_code = $_POST["product_code"];
$product_name = $_POST["product_name"];
$product_qty = $_POST["product_qty"];
$product_price = $_POST["price"];
$product_color = $_POST["product_color"];
$_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price);
}
print_r ($_SESSION['cart']);/*header("Location: view_cart.php");*/
if(isset($_POST['empty']) ){
unset($_SESSION["cart"]);
}
?>
<html>
<head>
</head>
<body>
<form method="POST" action="cart.php"><div align="center"><button type="remove" name="empty" >empty</button></div>
<a href="ppage.php">back </a>
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>code</th><th>colour</th><th>remove</th></tr></thead>
<tbody>
<?php
foreach($_SESSION['cart'] as $itm=>$val)
echo "<thead>";
echo"<tr>";
echo '<td>'.$val['$qnty'].'</td>';
echo '<td>'.$val['name'].'</td>';
echo '<td>'.$val['price'].'</td>';
echo '<td>'.$val['product_code'].'</td>';
echo '<td>'.$val['color'].'</td>';
/*echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>'
*/
if (isset($_POST['remove'])){
unset($_SESSION['cart'][$_POST['remove']]);}
/*echo <div align="right"><button type="remove" name="remove" >remove</button></div> */
echo '</tr>';
echo "<thead>";
?>
echo <div align="right"><button type="remove" name="remove" >remove</button></div>
echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>'
</form>
</tbody>
</body>
</html>
不需要在那里创建任何类型的 form
,只需为每一行创建一个超链接并将数组索引值附加到它,如下所示:
echo '<td><a href="?remove=' . $itm . '">remove</a></td>';
并且在处理过程中,只需使用 $_GET
superglobal 捕获数组索引值,然后从 $_SESSION['cart']
中删除项目数组,如下所示:
if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){
unset($_SESSION['cart'][$_GET['remove']]);
}
所以你的代码应该是这样的:
<?php
// Start session
session_start();
// Declare $_SESSION['cart'] as an empty array
if(empty($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
// Store all product details in $_SESSION['cart'] array
if(isset($_POST['product_code'])){
$product_code = $_POST["product_code"];
$product_name = $_POST["product_name"];
$product_qty = $_POST["product_qty"];
$product_price = $_POST["price"];
$product_color = $_POST["product_color"];
$_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price);
}
// Check whether the URL parameter 'remove' is set or not
// If it's set and not empty, then delete that item array from the $_SESSION['cart']
if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){
unset($_SESSION['cart'][$_GET['remove']]);
}
?>
<html>
<head>
</head>
<body>
<table width="100%" cellpadding="6" cellspacing="0" border="1" style="text-align:center;">
<thead>
<tr>
<th>Quantity</th>
<th>Name</th>
<th>Price</th>
<th>code</th>
<th>colour</th>
<th>remove</th>
</tr>
</thead>
<tbody>
<?php
// Check whether the cart is empty or not
if(count($_SESSION['cart'])){
// Loop through the $_SESSION['cart'] array
// and display the item details and 'remove' hyperlink
// for each row
foreach($_SESSION['cart'] as $itm=>$val){
echo '<tr>';
echo '<td>'.$val['qnty'].'</td>';
echo '<td>'.$val['name'].'</td>';
echo '<td>'.$val['price'].'</td>';
echo '<td>'.$val['product_code'].'</td>';
echo '<td>'.$val['color'].'</td>';
echo '<td><a href="?remove=' . $itm . '">remove</a></td>';
echo '</tr>';
}
}else{
echo '<tr><td colspan="6">No item in the cart</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>
我想用简单的 php 和 mysql 代码创建购物车,这里是查看购物车的代码,我想在 table 中查看商品,并带有删除每个按钮 在这里我创建了一个购物车并且看到了我想要一个简单的购物车所以任何人都可以告诉我如何在每个项目上显示一个删除按钮并在购物车中显示该项目
<?php
session_start();
include_once("config.php");
if(empty($_SESSION['cart']))
$_SESSION['cart'] = array();
if(isset($_POST['product_code'])){
$product_code = $_POST["product_code"];
$product_name = $_POST["product_name"];
$product_qty = $_POST["product_qty"];
$product_price = $_POST["price"];
$product_color = $_POST["product_color"];
$_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price);
}
print_r ($_SESSION['cart']);/*header("Location: view_cart.php");*/
if(isset($_POST['empty']) ){
unset($_SESSION["cart"]);
}
?>
<html>
<head>
</head>
<body>
<form method="POST" action="cart.php"><div align="center"><button type="remove" name="empty" >empty</button></div>
<a href="ppage.php">back </a>
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>code</th><th>colour</th><th>remove</th></tr></thead>
<tbody>
<?php
foreach($_SESSION['cart'] as $itm=>$val)
echo "<thead>";
echo"<tr>";
echo '<td>'.$val['$qnty'].'</td>';
echo '<td>'.$val['name'].'</td>';
echo '<td>'.$val['price'].'</td>';
echo '<td>'.$val['product_code'].'</td>';
echo '<td>'.$val['color'].'</td>';
/*echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>'
*/
if (isset($_POST['remove'])){
unset($_SESSION['cart'][$_POST['remove']]);}
/*echo <div align="right"><button type="remove" name="remove" >remove</button></div> */
echo '</tr>';
echo "<thead>";
?>
echo <div align="right"><button type="remove" name="remove" >remove</button></div>
echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>'
</form>
</tbody>
</body>
</html>
不需要在那里创建任何类型的 form
,只需为每一行创建一个超链接并将数组索引值附加到它,如下所示:
echo '<td><a href="?remove=' . $itm . '">remove</a></td>';
并且在处理过程中,只需使用 $_GET
superglobal 捕获数组索引值,然后从 $_SESSION['cart']
中删除项目数组,如下所示:
if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){
unset($_SESSION['cart'][$_GET['remove']]);
}
所以你的代码应该是这样的:
<?php
// Start session
session_start();
// Declare $_SESSION['cart'] as an empty array
if(empty($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
// Store all product details in $_SESSION['cart'] array
if(isset($_POST['product_code'])){
$product_code = $_POST["product_code"];
$product_name = $_POST["product_name"];
$product_qty = $_POST["product_qty"];
$product_price = $_POST["price"];
$product_color = $_POST["product_color"];
$_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price);
}
// Check whether the URL parameter 'remove' is set or not
// If it's set and not empty, then delete that item array from the $_SESSION['cart']
if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){
unset($_SESSION['cart'][$_GET['remove']]);
}
?>
<html>
<head>
</head>
<body>
<table width="100%" cellpadding="6" cellspacing="0" border="1" style="text-align:center;">
<thead>
<tr>
<th>Quantity</th>
<th>Name</th>
<th>Price</th>
<th>code</th>
<th>colour</th>
<th>remove</th>
</tr>
</thead>
<tbody>
<?php
// Check whether the cart is empty or not
if(count($_SESSION['cart'])){
// Loop through the $_SESSION['cart'] array
// and display the item details and 'remove' hyperlink
// for each row
foreach($_SESSION['cart'] as $itm=>$val){
echo '<tr>';
echo '<td>'.$val['qnty'].'</td>';
echo '<td>'.$val['name'].'</td>';
echo '<td>'.$val['price'].'</td>';
echo '<td>'.$val['product_code'].'</td>';
echo '<td>'.$val['color'].'</td>';
echo '<td><a href="?remove=' . $itm . '">remove</a></td>';
echo '</tr>';
}
}else{
echo '<tr><td colspan="6">No item in the cart</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>