所以我正在尝试使用 cart 创建一个应用程序,当我尝试添加该项目时,它不起作用。顺便说一句,我已经有一个工作购物车应用程序,这就是为什么我想知道为什么它不起作用。我几乎从工作中复制了所有内容。这是代码
购物车控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('cart');
}
public function add_to_cart(){
$id = $this->uri->segment(3);
if($this->cart->contents()){
foreach ($this->cart->contents() as $item){
if ($item['id']==$id){
$data = array('rowid'=>$item['rowid'],
'qty'=>++$item['qty']);
$process = $this->cart->update($data);
}
else{
$data = array(
'id'=>$id,
'qty'=>1,
'name' => $this->get_data->get_value('product_name','products','product_id', $id),
'price' => $this->get_data->get_value('product_price','products','product_id', $id)
);
$process = $this->cart->insert($data);
}
}
}
else{
$data = array('id'=>$id,
'qty' =>1,
'name' => $this->get_data->get_value('product_name','products','product_id', $id),
'price' => $this->get_data->get_value('product_price','products','product_id', $id),
);
$process = $this->cart->insert($data);
}
if($process){
$this->session->set_flashdata('success', 'Successful');
redirect('products');
}
else{
$this->session->set_flashdata('failed', 'Failed');
redirect('products');
//var_dump($process);
}
}
这是按钮
<div class="button pull-right" style="margin-top: 10px;"><a href="<?php echo base_url().'cart/add_to_cart/'.$row->product_id;?>"><span class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</a></div>
我真的看不到问题,我正在使用会话数据库,sess_us_database已经是真的。我尝试使用var_dump($process)
它是错误的,我尝试了var_dump($data)
,数据似乎很好,但插入部分不起作用。知道伙计们吗?这对我有很大帮助,谢谢。
CI 默认购物车只允许alpha-numeric, dashes, underscores, colons or periods
产品名称,如果产品价格0
,那么它也不会将产品添加到购物车。
请先检查它们。
更改此变量的一个好方法是使用以下代码对应用程序''库''MY_Cart.php进行MY_Cart.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Cart extends CI_Cart {
var $product_name_rules = '[:print:]';
}
检查您是否忘记了以下内容:
$items数组是否包含 ID、数量、价格和名称? 这些是必需的
1 - 价格必须是数字且大于 0。2 - 名称必须在 alpha 上3 - 数量为数字且大于 0 。4 - 必须设置 ID
如果您需要添加任何名称,则可以删除此代码:
if ($this->product_name_safe && ! preg_match('/^['.$this-product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
{
log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
return FALSE;
}
从系统/库/购物车.php : 第 227 行
当产品添加到购物车时,替换产品名称中的特殊字符。
示例:EKEN H9R/H9运动相机超高清4K/25fps WiFi 2.0" 170D水下防水头盔视频录制
// Trim special character from text
public function trim_special_char($text)
{
$str = str_replace("(", '_:', $text);
$str = str_replace(")", ':_', $str);
$str = str_replace("/", '_slash', $str);
$str = str_replace("+", '_plus', $str);
$str = str_replace("&", '_and', $str);
$str = str_replace("'", '_ss', $str);
$str = str_replace("x", '_X', $str);
$str = str_replace('"', '_cot', $str);
return $str;
}
// Set special character from previous text
public function set_special_char($text)
{
$str = str_replace('_:', "(", $text);
$str = str_replace(':_', ")", $str);
$str = str_replace('_slash', "/", $str);
$str = str_replace('_plus', "+", $str);
$str = str_replace('_and', "&", $str);
$str = str_replace('_ss', "'", $str);
$str = str_replace('_X', "x", $str);
$str = str_replace('_cot', '"', $str);
return $str;
}