我正在使用codeigniter cart和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*/
}
答案 0 :(得分:1)
public function product_add()
{
$id=$this->db->insert_id(); //its return last insert item on table
echo $id;
exit();
}
答案 1 :(得分:0)
你试过吗
$last_id = mysqli_insert_id($conn);