duanpen9294 2014-07-04 07:16
浏览 52

带参数的控制器中的调用方法

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

  }?>
  • 写回答

1条回答 默认 最新

  • douqiang3768 2014-07-04 07:19
    关注

    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')
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改