Udara Suranga
Udara Suranga

Reputation: 153

how to pass variable from one function to another function in codeigniter

I have two functions in my controller like this.

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);
}

I want to pass pro_id variable to download_function from search_keyword funtion.please help me to do this.

this is my model

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);
}

Upvotes: 0

Views: 2541

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94642

Its quite simple, do exactly what you would do to pass a paramter to any function.

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);
}

After some time in a chat room the solution we came up with was to chnage the code to this:

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);
}

Upvotes: 0

Giri Annamalai M
Giri Annamalai M

Reputation: 809

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);
}

You can pass it through parameter.

Upvotes: 1

Related Questions