Gihan Wijethunga
Gihan Wijethunga

Reputation: 85

call method in controller with parameters

I need to pass parameters to the controller, In the below I have mention a brief explanation with the code,

Controller

class Site2 extends CI_Controller
{

    function index()
    {
        $this->load->helper('url');
        $this->home();
    }

    public function home()
    {
        $this->load->model('get_company_model');
        $this->load->model('bank_account_model');
        $data['results'] = $this->get_company_model->get_All();
        $this->load->view('view_header');
        $this->load->view('view_nav',$data);
        $this->load->view('view_content');
        $this->load->view('view_footer');
    }

in the results array there are id and name,I can get these values in my view, then I need to pass id as parameters and call the function in controller.

view

<?php
    foreach($results as $row){

   echo $row->id;
    //I need to pass this particular id to the function

  }?>

Upvotes: 0

Views: 1087

Answers (1)

Nil&#39;z
Nil&#39;z

Reputation: 7475

You should do it like this:
1) If in same controller:

function home(){
    $this->load->model('get_company_model');
    $this->load->model('bank_account_model');
    $data['results'] = $this->get_company_model->get_All();
    $this->some_other_fn_in_same_controller( $data['results'] );
    $this->load->view('view_header');
    $this->load->view('view_nav',$data);
    $this->load->view('view_content');
    $this->load->view('view_footer');
}

function some_other_fn_in_same_controller( $results ){
    //to do
}

2) If from view:

2.1) Link:

<a href="<?php echo base_url() ?>controller/function/$results['id']">Some Link</a>

2.2) Form: Just put the id in a hidden field and post the form to the controller and get the id in the controller as usual:

<input type="hidden" name="id" value="<?php echo $result['id'] ?>">
$id = $this->input->post('id')

Upvotes: 1

Related Questions