0

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);
}
11
  • Can't you just do this: $this->download_function($pro_id); Commented Aug 26, 2015 at 10:18
  • Pass variable $this->download_function(VARIABLE); and getting using function download_function(VARIABLE) Commented Aug 26, 2015 at 10:18
  • @saty I do what you said. function download_function($pro_id) then I got an error which saying Missing argument 1 for Search::download_function() what should I do now? Commented Aug 26, 2015 at 10:26
  • @madforstrength I try that way. but then I got an error like this Missing argument 1 for Search::download_function() .can you please help me to solve this problem. Commented Aug 26, 2015 at 10:29
  • 1
    Post your model code!!! Commented Aug 26, 2015 at 10:45

2 Answers 2

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

You can pass it through parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

I try this. but then I got error like this Missing argument 1 for Search::download_function(). what should I do now.
If you want to access the function download_function, you must mention the argument. Like this, $pro_id=1; localhost/diectory/controllername/download_function/$pro_id
0

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

7 Comments

yep. I tried this.but I got an error. also can you please tell my why you put a 's' after $pro_id in second function.
$pro_ids the s pluralises the name indicating there is likely to be more that one of whatever this is. It is an array afterall. What error did you get
Actually thinking about it, you said this was a controller. Should you not have a public/protected/private designator on the function definitions! See code change
I got error like this Missing argument 1 for Search::download_function(). Please help me to solve this
Ahhhh I bet the database query is failing, or it is not returning what you think, and therefore the foreach is not running and therefore you are never actually creating the $pro_id variable. I uncommented the //$pro_id = []; try it now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.