[英]Product id of last inserted item in codeigniter shopping cart
我正在使用codeigniter購物車和ajax開發購物車。 當我將產品添加到購物車時,模式彈出窗口將顯示添加的產品的詳細信息。 我的問題是獲取最后插入的商品的產品ID。
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*/
}
public function product_add()
{
$id=$this->db->insert_id(); //its return last insert item on table
echo $id;
exit();
}
你試過了嗎
$last_id = mysqli_insert_id($conn);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.