gpt4 book ai didi

javascript - 限制用户在php中多次添加一件商品到购物车

转载 作者:行者123 更新时间:2023-11-29 16:03:09 25 4
gpt4 key购买 nike

我用 php 创建了一个网站。我在其中提供了购物车功能。当用户单击“添加到购物车”按钮时,他们应该被重定向到显示商品的购物车页面。处理购物车代码的library.php如下:

<?php

// load database connection script

include("database_connection.php");

/*
* Tutorial: PHP MySQL Shopping cart
*
* Page: Application library
* */

class ShopingCart
{



protected $db;

function __construct()
{
$this->db = DB();
}

/**
* get products list
*
* @return array
*/
public function getProducts()
{
$query = "SELECT * FROM `entertainment`";
if (!$result = mysqli_query($this->db, $query)) {
exit(mysqli_error($this->db));
}
$data = [];
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
}

return $data;
}

/**
* get given product details
*
* @param [integer] $id
* @return array
*/
public function getProductDetails($id)
{
$id = mysqli_real_escape_string($this->db, $id);
$query = "SELECT * FROM `entertainment` WHERE `id` = '$id'";
if (!$result = mysqli_query($this->db, $query)) {
exit(mysqli_error($this->db));
}
$data = [];
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data['id'] = $row['id'];
$data['title'] = $row['title'];
$data['price'] = $row['vendor_price'];
$data['quantity'] = 1;
}
}

return $data;
}

/**
* Add new product into the cart
*
* @param [integer] $id
* @return void
*/
public function addToCart($id)
{

$product = $this->getProductDetails($id);

$isFound = false;
$i = 0;

if (!isset($_SESSION['shopping_cart']) || count($_SESSION['shopping_cart']) < 1)
{
$_SESSION['shopping_cart'] = array(0 => $product);
} else {

foreach ($_SESSION['shopping_cart'] as $item) {
$i++;
foreach ($item as $key => $value) {
if ($key == "id" && $value == $id) {
array_splice($_SESSION['shopping_cart'], $i - 1, 1, array([
'id' => $item['id'],
'title' => $item['title'],
'price' => $item['vendor_price'],
'quantity' => $item['quantity'] + 1,
]));
$isFound = true;
}
}

}
if ($isFound == false) {
array_push($_SESSION['shopping_cart'], $product);
}
}

}

/**
* remove existing product from the cart
*
* @param [integer] $id
* @return void
*/
public function removeProductFromCart($id)
{
unset($_SESSION['shopping_cart'][$id - 1]);
}


}

?>

我的购物车 php 文件如下所示:

<?php
if(isset($_SESSION['shopping_cart']) && count($_SESSION['shopping_cart']) > 0)
{
$entertainment = $_SESSION['shopping_cart'];

echo '
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Quantity</th>
<th scope="col">Price</th>
<th scope="col" width="100">Action</th>
</tr>
</thead>';

$item_number = 1;
$total = 0;
foreach ($entertainment as $product) {
echo '
<tbody>
<tr>
<th scope="row">'. $item_number .'</th>
<td>' . $product['title'] . '</td>
<td>'.$product['quantity'].'</td>
<td>₹ '. $product['price']. '</td>
<td>
<a href="cart.php?id_to_remove=' . $item_number . '" class="btn btn-danger btn-sm">X</a>
</td>
</tr>
</tbody>
';
$total += ((int)$product['price'] * $product['quantity']);
$item_number++;
}

echo '
<tr>
<th colspan="4" align="right">
Total:
</th>
<td>
₹ '. $total .'
</td>
</tr>
</table>';

}

其实这个购物车是我引用网上资料制作的。购物车工作正常。但问题是我需要限制用户多次添加一个项目,用户只能添加一个项目一次,即该项目的数量应该只有1,当用户尝试多次添加一个项目时,他们应该引起警惕。

谁能告诉我如何解决这个问题?

最佳答案

您可以通过添加以下内容来消除值数组中的重复值:

array_unique($yourArray);

如果您的数组具有这些值:

$yourArray = ['a','b','c','b'];
$array = array_unique($yourArray);
//$array = ['a','b','c'];

关于javascript - 限制用户在php中多次添加一件商品到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55983173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com