我已經在php中創(chuàng)建了一個網站。我已經在其中提供了購物車功能。當用戶單擊添加到購物車按鈕時,應將他們重定向到顯示商品的購物車頁面。下面是處理購物車代碼的library.php:<?php// load database connection scriptinclude("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;實際上,我已經參考了互聯(lián)網來制作此購物車。手推車工作正常。但是問題是我需要限制用戶多次添加一個項目,用戶只能添加一個項目一次,也就是說,該項目的數(shù)量應該僅為1,當用戶嘗試向一個項目添加更多次時,他們應該警惕。誰能告訴我該如何解決?
1 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
通過添加以下內容,U可以從值數(shù)組中消除重復的值:
array_unique($yourArray);
如果您的數(shù)組具有這些值:
$yourArray = ['a','b','c','b'];
$array = array_unique($yourArray);
//$array = ['a','b','c'];
- 1 回答
- 0 關注
- 153 瀏覽
添加回答
舉報
0/150
提交
取消