问题描述
我刚刚开始试用CodeIgniter(版本2.1.4)的购物车类,并且一直在关注有助于对其进行解释的教程。但是由于某种原因,我无法成功向购物车添加一个简单的数组。
I've just started playing around with the shopping cart class for CodeIgniter (version 2.1.4) and I've been following tutorials that help explain it. But for some reason I am unable to successfully add a a simple array to the cart.
这是我对购物车类的实现:
Here is my implementation of the cart class:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart extends CI_Controller {
public function __construct() {
parent:: __construct();
}
public function index() {
$nav["navigation"] = $this->getCategory->getCategories();
$this->load->view("header");
$this->load->view("scripts");
$this->load->view("nav", $nav);
$this->load->view("cart");
$this->load->view("footer");
}
function add() {
$data = array(
"id" => "42",
"name" => "pants",
"quantity" => 1,
"price" => 19.99,
"options" => array("size" => "medium")
);
$this->cart->insert($data);
var_dump($this->cart->contents()); //This should output the array!
}
function show() {
$cart = $this->cart->contents();
echo "<pre>" . print_r($cart) . "</pre>"; //Nothing here either!
}
function update() {
$this->cart->update($data);
redirect(base_url()."cart");
}
function total() {
echo $this->cart->total();
}
function remove() {
$this->cart->update($data);
}
function destroy() {
$this->cart->destroy();
}
}
但是,如果我转到添加函数,则var_dump仅显示 array(0){}。
But if I go to the add function the var_dump just displays "array(0) { }". Same result if I navigate to the show function.
这是我的自动加载配置,显示我已自动加载购物车库:
Here is my autoload config showing that I have autoloaded the cart library has been loaded:
$autoload['libraries'] = array("database", "session", "cart");
$autoload['helper'] = array("html", "url", "form");
我知道这很简单,很明显我很想念,但是现在我就离开了莫名其妙。
I know it's something really simple and obvious I'm missing, but right now I'm just left baffled. Any suggestions?
推荐答案
您用于数量
的数组键的命名不正确。在数据中,您要插入的数量为 qty
表示要保存到购物车的数据。
Your array key for quantity
is named improperly. In the data your are inserting the quantity must be named as qty
for the data to be saved to the cart.
"qty" => 1,
这篇关于无法添加到购物车类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!