我正在尝试将一些数据添加到 codeigniter (HMVC codeigniter) 切菜车并显示它,我在主购物车控制器中使用此方法:
function add_to_cart(){
$this->load->library('cart');
// Get data
$userID = $this->input->post('userID');
$eventID = $this->input->post('eventID');
$tickedID = $this->input->post('tickedID');
// Get ticket data
$this->load->module('ticket');
$ticket_query = $this->ticket->get_where($tickedID);
//echo $this->session->all_userdata();
foreach($ticket_query->result() as $ticket_data){
$ticketPrice = $ticket_data->price;
$ticketCategory = $ticket_data->category;
}
//echo 'tickedID: '.$tickedID.' price: '.$ticketPrice.' category: '.$ticketCategory;
// Add item to cart
$data_items = array(
'id' => $tickedID,
'qty' => 1,
'price' => $ticketPrice,
'category' => $ticketCategory,
'options' => array()
);
$this->cart->insert($data_items);
$cart = $this->cart->contents();
echo '<pre>';
echo print_r($cart);
echo '</pre>';
}
基本上我从会话中获取userID
,eventID
和tickedID
变量,然后我运行查询以获取带有特定 ID 的勾选。我遍历查询结果并从中获取$thicketPrice
和$ticketCategory
变量。然后我尝试设置变量$data_items
以插入购物车本身。最后我尝试echo
照顾的内容,我得到的只是一个空数组。
会话、数据库和购物车库都是自动加载的,并且会话正在使用数据库,它们有ci_sessions
表。会话也有一个加密密钥,有什么问题?