php CI $this->cart->insert($data) 获取空值

php CI $this->cart->insert($data) get null value

我在脚本中得到堆栈。我使用了 Code Igniter 版本。 3.1.10。我在我的控制器中使用购物车库

这是我的控制器

    public function add_to_cart()
        {
            $idit=$this->input->post('id_item');
            $product=$this->Salesmodel->get_item($idit);
            $i=$product->row_array();
            $data = array(
                   'id'             => $i['id_item'],
                   'name'           => $i['name_item'],
                   'main_price'     => $i['main_price'],
                   'sell_price'     => $i['sell_price'],

                );

            $this->cart->insert($data);
            $rows = count($this->cart->contents());    // I want to find out rows count and result is null
            echo $i['id_item'];                     //get value, not null
            echo $rows;                                // get '0'

        }

model.php

function get_item($idit)
{
   $rslt=$this->db->query("SELECT * FROM tb_item where id_item='$idit'");
   return $rslt;
}

但在该脚本中,我总是得到购物车的空行数。 我必须在 config.php 中添加此脚本:

$config['sess_use_database'] = TRUE;

我还创建了一个名为

的新table

ci_session

但是 returns 结果相同,我的购物车总是有空行计数和空数据。请帮助我解决我制作的脚本中的错误。

提前致谢

为了正确保存到购物车,这 4 个数组索引是必需的 :
id - 项目标识符。
qty - 物品数量。
price - 商品价格。
name - 项目名称。

而第 5 个索引是 options,您可以在其中存储您需要的所有附加属性(尽管应该是一个数组)。

因此您可以像这样修改 $data 数组:

        $data = array(
               'id'             => $i['id_item'],
               'qty'            => 1, // here I just manually set it to 1
               'name'           => $i['name_item'],
               'price'          => $i['main_price'], // here I changed 'main_price' index to 'price'
               'options'        => array('sell_price' => $i['sell_price']) // moved the 'sell_price' array here

            );