Php 在控制器中使用参数调用方法
我需要将参数传递给控制器,在下面的代码中我提到了一个简短的解释 控制器Php 在控制器中使用参数调用方法,php,codeigniter,Php,Codeigniter,我需要将参数传递给控制器,在下面的代码中我提到了一个简短的解释 控制器 class Site2 extends CI_Controller { function index() { $this->load->helper('url'); $this->home(); } public function home() { $this->load->model('get_comp
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作为参数传递,并在controller中调用函数
查看
<?php
foreach($results as $row){
echo $row->id;
//I need to pass this particular id to the function
}?>
你应该这样做:
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)链接:
2.2)表单:只需将id放入一个隐藏字段,然后将表单发布到控制器,并像往常一样在控制器中获取id:
<input type="hidden" name="id" value="<?php echo $result['id'] ?>">
$id = $this->input->post('id')
在我看来,循环中有一个特定的id,我需要将该id传递给foreach循环中controller中的函数。在controller中调用函数后,放置
标记?。是否要处理某些代码并在同一视图上显示?如果要在同一视图中加载数据而不加载页面,则最好在模型中使用函数而不是在控制器中使用函数。
<input type="hidden" name="id" value="<?php echo $result['id'] ?>">
$id = $this->input->post('id')