简体   繁体   English

在购物车Codeigniter中添加并显示列表产品

[英]Add and show list product in cart codeigniter

I have a problem when I add some products to cart, but then when I want to fetch contents of the cart I find it is empty. 将某些产品添加到购物车时出现问题,但是当我想获取购物车的内容时,发现它为空。

this is my controller : 这是我的控制器:

public function add_to_cart(){

    $insert_data = array( 
     'id' => $this->input->post('id'),
     'name' => $this->input->post('name'),
     'price' => $this->input->post('price'),
     'qty' => $this->input->post('qty') );

     // This function add items into cart.
    $this->cart->insert($insert_data);


}

and this is my form to add new product to cart : 这是我将新产品添加到购物车的表格:

<div class="button-group">
                  <form method="POST" action="<?php echo base_url().'add_to_cart'; ?>">
                  <div style="display:none">
                  <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
                  </div> 
                  <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
                    <input type="number" name="qty" id="<?php echo $ligneBase->id;?>">
                    <input type="hidden" name="name" value="<?php echo $ligneBase->name;?>">
                    <input type="hidden"  name="price"  value="<?php echo $ligneBase->price;?>">
                    <input type="hidden"  name="id"  value="<?php echo $ligneBase->id;?>">
                      <input class="add_cart" type="submit" value="Add to cart">
                      <div class="add-to-links">
                  </form>
                </div>

and this is my cart in header.php : 这是我在header.php中的购物车:

<table class="table">
                <tbody>
                <div id="text">
                  <?php $cart_check = $this->cart->contents();

                  // If cart is empty, this will show below message.
                  if(empty($cart_check)) {
                  echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
                  } ?> 
                </div>

                  <?php
                     $cart = $this->cart->contents();
                     foreach($cart as $indice => $ligneBase){
                  ?>
                  <tr>
                    <td class="text-center"><a href="product.html"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></a></td>
                    <td class="text-left"><a href="product.html"><?php echo $ligneBase->id;?></a></td>
                    <td class="text-right">x 1</td>
                    <td class="text-right"><?php echo $ligneBase->name;?> </td>
                    <td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
                  </tr>

                <?php
                  }
                ?>


                </tbody>
              </table>

my workout in your code 我在您的代码中锻炼

my controller 我的控制器

public function workout()
    {



        if($this->input->post())
        {
            //echo "<pre>"; print_r($this->input->post()); 

            $insert_data = array( 
     'id' => $this->input->post('id'),
     'name' => $this->input->post('name'),
     'price' => $this->input->post('price'),
     'qty' => $this->input->post('qty') );

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

            echo "<pre>"; print_r($this->cart->contents()); 


            exit();
        }

        $this->load->view('workout');
    }

my view 我的观点

<form method="POST" action="<?php echo base_url().'welcome/workout'; ?>">
                    <input type="number" name="qty" id="1002">
                    <input type="hidden" name="name" value="Mobile">
                    <input type="hidden"  name="price"  value="300">
                    <input type="hidden"  name="id"  value="1002">
                      <input class="add_cart" type="submit" value="Add to cart">
                      <div class="add-to-links">
                  </form>

and my output is 我的输出是

Array
(
    [fba9d88164f3e2d9109ee770223212a0] => Array
        (
            [id] => 1002
            [name] => Mobile
            [price] => 300
            [qty] => 24
            [rowid] => fba9d88164f3e2d9109ee770223212a0
            [subtotal] => 7200
        )

)

works fine 工作正常

在此处输入图片说明

and your error is in displaying foreach 并且您的错误在于显示foreach

<table class="table">
                <tbody>
                <div id="text">
                  <?php $cart_check = $this->cart->contents();

                  // If cart is empty, this will show below message.
                  if(empty($cart_check)) {
                  echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
                  } ?> 
                </div>

                  <?php
                     $cart = $this->cart->contents();
                     foreach($cart as $indice => $ligneBase){
                  ?>
                  <tr>
                    <td class="text-center"><a href="product.html"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></a></td>
                    <td class="text-left"><a href="product.html"><?php echo $ligneBase->id;?></a></td>
                    <td class="text-right">x 1</td>
                    <td class="text-right"><?php echo $ligneBase->name;?> </td>
                    <td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
                  </tr>

                <?php
                  }
                ?>


                </tbody>
              </table>

change it to 更改为

<table class="table">
                    <tbody>
                    <div id="text">
                      <?php $cart_check = $this->cart->contents();

                      // If cart is empty, this will show below message.
                      if(empty($cart_check)) {
                      echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
                      } ?> 
                    </div>

                      <?php
                         $cart = $this->cart->contents();
                         foreach($cart as $indice => $ligneBase){
                      ?>
                      <tr>
                        <td class="text-center"><a href="product.html"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></a></td>
                        <td class="text-left"><a href="product.html"><?php echo $ligneBase['id'];?></a></td>
                        <td class="text-right">x 1</td>
                        <td class="text-right"><?php echo $ligneBase['name'];?> </td>
                        <td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
                      </tr>

                    <?php
                      }
                    ?>


                    </tbody>
                  </table>

Can I suggest a bit improvements in your code structure. 我可以建议您对代码结构进行一些改进。 You need to use a model for db interactions 您需要使用模型进行数据库交互

Step-1. 第1步。 Controller Function 控制器功能

public function cart()
{
    $data['cart_items']=$this->cart_model->getCartItems();
    if($_POST)
    {
        // Perform Validation
        if($this->form_validation->run()==false)
        {
            $data['errors']=validation_errors();
            $this->load->view('cart_view',$data);
        }
        else
        {
            $row=$this->cart_model->insertToCart($this->security->xss_clean($_POST));
            if($row)
            {
                $data['success']='Item Added';
                $this->load->view('cart_view',$data);
            }
            else
            {
                $data['error']='Item Could not be Added';
                $this->load->view('cart_view',$data);
            }
        }
    }
    else
    {
        $this->load->view('cart_view',$data);
    }

}

Step-2. 第2步。 Model Functions 模型功能

public function getCartItems()
{
    $sql='create query';
    return $this->db->query($sql)->result_array();
}

public function insertToCart($data)
{
    $item=array(
        'field' => $data['index']
    );
    $this->db->insert('cart',$item);
    return $this->db->insert_id();
}

Step-3. 步骤3。 View 视图

<div class="button-group">
     <form method="POST" action="">
       <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
       <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
       <input type="number" name="qty" id="<?php echo $ligneBase->id;?>">
       <input type="hidden" name="name" value="<?php echo $ligneBase->name;?>">
       <input type="hidden"  name="price"  value="<?php echo $ligneBase->price;?>">
       <input type="hidden"  name="id"  value="<?php echo $ligneBase->id;?>">
       <input class="add_cart" type="submit" value="Add to cart">
       <div class="add-to-links">
      </form>
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM