2017-05-31 98 views
0

我正在開發使用codeigniter購物車和ajax的購物車。當我將產品添加到購物車時,模式彈出窗口會顯示添加產品的詳細信息。我的問題是要獲取最後插入項目的產品ID。codeigniter購物車中最後插入商品的商品編號

Ajax代碼

$(".add_to_cart").click(function(event) { 

    var id=$(this).data('id'); 

    var qty=$("#item_"+id).val(); 

    $.ajax({ 
      type: 'POST', 
      url: '<?php echo base_url("ajax_controller/add_to_cart/'+id+'/'+qty+'")?>', 
      data: { id:id }, 
      success:function(response){ 
      $("#total_items").html(response); 
      /*$(".view_cart").click();*/ 
      $(".product_add").click(); 
    } 
    });/* Aajx */ 

});/* Add to cart clicked */ 

$(".product_add").click(function(event) { 

$.ajax({ 
      type: 'POST', 
      url: '<?php echo site_url("ajax_controller1/product_add")?>', 
      data: { id:'1' }, 
      success:function(response){ 
       $("#cart_container").html(response); 
       $("#myModal_cart").modal('show'); 
    } 
    });/* Aajx */ 



}); 
<li style=""><a href="javascript:void(0);" class="product_add" >Last Item</a></li> 

和我的Ajax控制器

public function add_to_cart($pid,$qty) 
    { 
       $this->load->library('cart'); 

     $this->load->model('product_model'); 
     $query=$this->product_model->get_product($pid); 

     foreach ($query->result() as $row) 
     { 
      $name=$row->product_name; 
      $price=$row->price; 
      $img=$row->img_name; 
     } 


      $data = array(
       'id'  => $pid, 
       'qty'  => $qty, 
       'price' => $price, 
       'name' => $name, 
       'options' => array('Status' => 'New') 
      ); 

      $this->cart->insert($data); 
      $lid = $this->db->insert_id(); 

      echo count($this->cart->contents()); 
    } 
    public function product_add() 
    { 
    /*in this controler i need to get id of last inserted product*/ 
    } 

回答

0

你有沒有試過

$last_id = mysqli_insert_id($conn); 
1
public function product_add() 
{ 
    $id=$this->db->insert_id(); //its return last insert item on table 
    echo $id; 
    exit(); 
} 
相關問題