我的控制器中有两个这样的功能。
function search_keyword()
{
$keyword = $this->input->post('keyword');
$this->data['result'] = $this->mymodel->search($keyword);
//print_r($this->data);
//$pro_id = [];
foreach($this->data['result'] as $key => $val){
$pro_id[] = $val->product_id;
}
print_r($pro_id);
$this->download_function();
$this->twig->display('home.html', $this->data);
}
function download_function()
{
//print_r($keyword);
$this->data['re'] = $this->csv->ExportCSV($keyword);
$this->twig->display('home.html', $this->data);
}
我想通过search_keyword函数将pro_id变量传递给download_function。请帮助我这样做。
这是我的模特
function ExportCSV($pro_id)
{
//print_r($keyword);
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename_you_wish.csv";
$query = "SELECT * FROM products WHERE product_id LIKE '%$pro_id%'";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
}
答案 0 :(得分:1)
function search_keyword()
{
$keyword = $this->input->post('keyword');
$this->data['result'] = $this->mymodel->search($keyword);
//print_r($this->data);
//$pro_id = [];
foreach($this->data['result'] as $key => $val){
$pro_id[] = $val->product_id;
}
print_r($pro_id);
$this->download_function($pro_id);
$this->twig->display('home.html', $this->data);
}
function download_function($pro_id)
{
//print_r($keyword);
$this->data['re'] = $this->csv->ExportCSV($keyword);
$this->twig->display('home.html', $this->data);
}
您可以通过参数传递它。
答案 1 :(得分:0)
它非常简单,完全按照将参数传递给任何函数的方式。
public function search_keyword() {
$keyword = $this->input->post('keyword');
$this->data['result'] = $this->mymodel->search($keyword);
//print_r($this->data);
$pro_id = [];
foreach($this->data['result'] as $key => $val){
$pro_id[] = $val->product_id;
}
print_r($pro_id);
$this->download_function($pro_id);
$this->twig->display('home.html', $this->data);
}
public function download_function($pro_ids) {
//print_r($keyword);
$this->data['re'] = $this->csv->ExportCSV($keyword);
$this->data['pro_ids'] = $pro_ids;
$this->twig->display('home.html', $this->data);
}
在聊天室中待了一段时间之后,我们提出的解决方案是将代码更改为:
function search_keyword()
{
$pro_id = $this->input->post('keyword');
$this->download_function($pro_id);
$this->twig->display('home.html', $this->data);
}
function download_function($pro_id)
{
$this->data['re'] = $this->csv->ExportCSV($pro_id);
$this->twig->display('home.html', $this->data);
}