![](/img/trans.png)
[英]How to get last inserted id via Contact Form 7, and then assign it to a global variable
[英]How to get last inserted id, then assign it to a global variable
我想獲取最后插入的ID,然后將其分配給全局變量以供以后使用。
我使用callback_after_insert
如下:
$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));
function _callback_get_lastInsertID($post_array,$primary_key) {
$this->lastInsertedId = $primary_key;
}
然后,當使用$this->lastInsertedId
來獲取其值時,但我找不到存儲的任何值。
function featured_ad_offer() {
echo $this->lastInsertedId;
@$data['content'] .= $this->load->view('featured_ad_offer', '', true);
$this->load->view('index', $data);
}
有一條錯誤消息,指出Undefined property: Content::$lastInsertedId
現在,該怎么做?
請嘗試在“聲明”類本身之后立即在類中聲明變量(假設兩個函數都在同一類中),即
class Blog extends CI_Controller {
private $lastInsertedId = NULL;
public function fcn1() {$this->lastInsertedId = 3.14;}
public function fcn2() {var_dump($this->lastIndertedId);}
}
如果你真的是用它作為global variable
需要聲明它CI_Controller
,或者更好的創建文件/core/Public_Controller
延伸CI_Controller
,並讓所有的控制器可以通過不延長CI_Controller
但Public_Controller
因此,你可以很容易地聲明是變量“全球”。
如果你有麻煩在這里是一個用戶指南鏈接 。
還有另一個選項(不推薦!) config class ,
$this->config->set_item('item_name', 'item_value'); //setting a config value
$this->config->item('item name'); //accesing config value
也許您可以使用它來獲取最后插入的ID
$this->db->insert_id();
將其存儲在會話中
$this->load->library('session');
$this>session->set_userdata('last_inserted_id');
並在其他地方使用此會話變量
$last_inserted_id = $this->session->userdata('last_inserted_id');
希望這個幫助:D
您需要的解決方案可以通過Codeigniter會話輕松解決。 因此,例如,您可以做的是:
$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));
function _callback_get_lastInsertID($post_array,$primary_key) {
$this->session->set_userdata('last_insert_id', $primary_key);
}
確保首先加載了會話類 。 最好的方法是自動加載會話庫。
現在要再次實際調用last_insert_id,您只需通過調用即可:
echo $this->session->userdata('last_insert_id');
因此,在您的示例中,您可以簡單地執行以下操作:
function featured_ad_offer() {
echo $this->session->userdata('last_insert_id');
@$data['content'] .= $this->load->view('featured_ad_offer', '', true);
$this->load->view('index', $data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.