Codeigniter 中的购物车问题

Shopping Cart Issue in Codeigniter

我正在尝试在 CodeIgniter 中创建一个购物车,我尝试了文档中提到的方法并且效果很好。但是当我尝试在模板中使用它时,它不起作用。在文档中,产品是硬编码的,但我从数据库中获取产品并使用 ajax.

插入它

我的插入代码JS

$(document).ready(function(){
           $('.add-to-cart').click(function(){
              var product_id=$(this).data("productid");
              var product_name=$(this).data("productname");
              var product_price=$(this).data("productprice");
               var quantity=$('#' + product_id).val();
               if(quantity !='' && quantity>0)
                   {
                       $.ajax({
                           url:"<?php echo base_url();?>Cart/insert_product",
                           method:"POST",
                           data:{
                               product_id:product_id,product_name:product_name,product_price:product_price,quantity:quantity
                           },
                           success:function(data)
                           {
                                alert("Product added into cart");
                          // alert("Product added into cart and product info is: ID:" + product_id +'\n\r name:' + product_name + '\n\r price:' + product_price + '\n\r quantity:' + quantity);
                             //  $('#' + product_id).val('');
                       }
                       });
                   }
               else
                   {
                       alert("Please enter quantity");
                   }
           });
        });

我使用警报对其进行测试,以确保在我单击 "Add To Cart" 按钮时产品正在拣选。

我的控制器Insert_Product

// my cart
    public function insert_product()
    {
          $data=array(
                'id'      => $_POST['product_id'],
                'qty'     => $_POST['quantity'],
                'price'   => $_POST['product_price'],
                'name'    => $_POST['product_name']
        );
        // Inesrting Items into Cart
            $this->cart->insert($data);
    }

当我点击 "Add To Cart" 按钮时,它显示成功消息,但我很确定该产品没有插入购物车...

我尝试访问产品的地方。

 <!-- cart items -->
                      <div class="nav-cart-items">
                        <?php $i = 1; 
                          if(!$this->cart->contents()){echo "no items found in cart";}
                          foreach ($this->cart->contents() as $items): 
                         var_dump($items);
                          if(empty($items)){echo "cart is empty";}else{ echo "<h1>cart has data</h1>";?>
                        <div class="nav-cart-item clearfix">
                         <h4>Cart Details</h4>
                          <div class="nav-cart-img">

                            <a href="#">
                              <img src="cart_small_1.jpg" alt="">
                            </a>
                          </div>
                          <div class="nav-cart-title">
                            <a href="#">
                              <?php echo $items['name'];?>
                            </a>
                            <div class="nav-cart-price">
                              <span><?php echo $items['qty'];?> x</span>
                              <span><?php echo $this->cart->format_number($items['price']);?></span>
                            </div>
                          </div>
                          <div class="nav-cart-remove">
                            <a href="#"><i class="ui-close"></i></a>
                          </div>
                        </div>
                        <?php $i++; ?>
                    <?php } endforeach;?>
                      </div> <!-- end cart items --> 

如果购物车中没有商品,我会回复消息 "no items found in cart",它会向我显示这一点。我也试试这个代码

<?php
ob_start();
   var_dump($this->cart->contents());
   $result = ob_get_contents();
?>

测试内容,它显示 output :

F:\xampp\htdocs\cart\application\views\single_item.php:3:
array (size=0)
  empty

至于我觉得我做错了什么,但不知道在哪里... 还请在 CodeIgniter 中建议一个购物车解决方案,因为在文档中 Cart 库已弃用。

如有任何帮助,我们将不胜感激。

为什么在Controller中使用了$data。 如果我没记错,那么您是单独发送值,而不是在“$data 多关联数组”

$data=array(
              array(
                'id'      => $_POST['product_id'],
                'qty'     => $_POST['quantity'],
                'price'   => $_POST['product_price'],
                'name'    => $_POST['product_name'])
        );

尝试删除 $data=array() 并使用以下代码:

 $id = $this->input->post('product_id');
        $quantity = $this->input->post('quantity');
       $product_price = $this->input->post('product_price');
        $product_name = $this->input->post('product_name');

PHP 个脚本,即:<?php echo base_url();?> 将不会在 .js 个文件中执行。因此,在评论中,您的 url 是如何逐字显示在开发工具中的。

您可以将 js 文件的内容移动到您的 header 模板,以便它驻留在视图中。或者您可以在 js 中自己将 base_url() 定义为 var (var base_url = 'http://somesite.com/';) 并像 base_url 一样使用它或执行以下操作:

url:"/cart/insert_product" 假设 cart 不在 controllers

的子目录中

我通常会这样获取post数据并插入到table中:

 $productId = $this->security->xss_clean($this->input->post('product_id'));
 $quantity = $this->security->xss_clean($this->input->post('quantity'));
 $productPrice = $this->security->xss_clean($this->input->post('product_price'));
 $productName = $this->security->xss_clean($this->input->post('product_name'));


    $data = array(
       "id" => $productId,
       "qty" => $quantity,
       "price" => $productPrice,
       "name" => $productName
    );

    $this->cart->insert($data);