カートを作成するための正しい軌道に乗っています。リクエスト (セッション変数のカート) に従って、サンプル コードとメモを次に示します。
以下について、codeigniter のドキュメントを確認する必要があります。
1.セッション
2.ユーザー入力の取得方法
重要
CodeIgniter 以外のフレームワークを使用したくない場合でも、以下を確認する必要があります。
最近追加されたアイテムの数量は、製品の在庫を超えてはなりません
価格がデータベース内の実際の価格と等しいことを検証する必要があります (この最後のポイントは、システムのニーズによって異なります)。
ラストノート
カートをデバッグするには、次のコード行を使用できます
print_r($this->session->userdata('cart_products'));
これをビューまたはコントローラーに送信すると、カートがセッション変数に適切に保存されているかどうかが表示されます。
これは CodeIgniter のみのソリューションであることに注意してください。
そして、ここでのスピーチはすべてコードです。これがお役に立てば幸いです。
/*Your function*/
public function add_cart(){
/*We need to get the data from the user either via AJAX or via form submit*/
$md5Id = md5($this->input->post("id"));
$name = $this->input->post("name");
/*We need to make sure the data received is a number*/
if($this->input->post("qty")>=1){
/*To prevent tricky data we need to round numbers to prevent any undesirable stuff here*/
$qty = ceil($qty);
}else{
$qty = 1;
}
/*You need to get the price from the DATABASE, for the excersice I will use a fixed price*/
$price= 1500;
/*Retreive the cart from the session*/
$userCart = $this->session->userdata('cart_products');
/*Validate if the user has a cart*/
if($userCart!=null){
/*We check if the user added the item before*/
$findItem = $this->findItem($userCart, $md5Id);
/*If findItem is not equals 0*/
if($findItem!=0){
/*We declare the variable of the index to remove*/
$cartIndex = $findItem["arrayIndex"];
/*We get the qty of the item*/
$qtyItem = $userCart[$cartIndex]["qty"];
/*We create the new sum of the */
$qty = $qty + $qtyItem;
/*We establish the new qty for that item*/
$userCart[$cartIndex]["qty"] = $qty;
}
/*We need to replace the older cart with the new one*/
$this->session->set_userdata('cart_products', $userCart);
}else{
$userCart = array();
$item = array(
'id' => $md5Id,
'name' => $name,
'qty' => $qty,
'price' => $price,
);
/*In case the user does not have a cart we create one*/
$userCart[] = $item;
$this->session->set_userdata('cart_products', $userCart);
}
}
/*Search item in cart session*/
public function search_cart(){
/*We need to get the data from the user either via AJAX or via form submit*/
$name = $this->input->post("name");
/*Retreive the cart from the session*/
$userCart = $this->session->userdata('cart_products');
/*Validate if the user has a cart*/
if($userCart!=null){
/*We check if the user added the item before*/
$findItems = $this->findByName($userCart, $name);
/*If findItems is not equals 0*/
if($findItems!=0){
/*We return the items found in the cart as per search criteria, this is returned in json in case this needs to be readed via AJAX for example AND we use ECHO in case this thing is the controller*/
echo json_encode($findItems);
}
}
}
/*Function to find the cart item*/
public function findItem($cart, $md5Id){
/*We iterate over the cart*/
foreach($cart as $key=>$item){
/*Check if any id in the cart match your $md5Id*/
if($item["id"]==$md5Id){
/*In case this is found we return an array with the index of the array and qty*/
return array("arrayIndex"=>$key, "qty"=>$item["qty"]);
}
}
/*In case there is no item found in the cart*/
return 0;
}
/*Function to find the cart when user types a search criteria*/
public function findByName($cart, $userInput){
$itemsFound = array();
/*We iterate over the cart*/
foreach($cart as $key=>$item){
/*Check if any id in the cart match via user Input*/
if (strpos($item["name"], $userInput) !== false) {
$itemsFound[] = $item;
}
}
/*If is not empty the items found we return the array of items*/
if(!empty($itemsFound)){
return $itemsFound;
}
/*In case there is no item found in the cart*/
return 0;
}