我需要将参数传递给控制器。在下面我已经提到了代码的简要说明,
控制器
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');
}
在结果数组中有id和name,我可以在我的视图中获取这些值,然后我需要传递id作为参数并在控制器中调用该函数。
查看
<?php
foreach($results as $row){
echo $row->id;
//I need to pass this particular id to the function
}?>
答案 0 :(得分:1)
你应该这样做:
1)如果在同一个控制器中:
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)如果来自视图:
2.1)链接:
<a href="<?php echo base_url() ?>controller/function/$results['id']">Some Link</a>
2.2)表单:只需将id放入隐藏字段并将表单发布到控制器并像往常一样在控制器中获取id:
<input type="hidden" name="id" value="<?php echo $result['id'] ?>">
$id = $this->input->post('id')