简体   繁体   English

将数据从视图传递到控制器(codeigniter)

[英]Passing Data from View to Controller (codeigniter)

I am trying to pass some Data using a link from the view to the Controller. 我正在尝试使用从视图到控制器的链接传递一些数据。

Controller : 控制器

public function details(){
    echo $this->input->post('data_id');
    $data['details'] = $this->wapi_db->details($this->input->post('data_id'));
    $data['latest_news'] = $this->wapi_db->latest_news();
    $data['main_content'] = "category/details";
    $this->load->view('includes/template',$data);
}

View : 查看

<a href=<?php.base_url().'wapi/details?data_id='6'; ?>

You could simply us Uri segmen, just like this: 您可以简单地使用Uri段,如下所示:

your View Code for href. 您的href的查看代码。

<a href="<?php echo base_url() ?>wapi/details/6";

And to GET the value you passed from view to controller You should do something like this in your controller 并获取从视图传递到控制器的值,您应该在控制器中执行以下操作

    public function details(){

        $data_id = $this->uri->segment(3); //this where you get you value from your link
        ...//rest of your code

    }

I recommend you to use this link on your view 我建议您在视图中使用此链接

<a href=<?php.base_url().'wapi/details/6'; ?>

Then on your controller you just wait for the parameter on the function 然后在控制器上,您只需等待函数中的参数

public function details($data_id){//<- the parameter you want

    $data['details'] = $this->wapi_db->details($data_id);
    $data['latest_news'] = $this->wapi_db->latest_news();
    $data['main_content'] = "category/details";

    $this->load->view('includes/template',$data);

}

If your URI contains more than two segments they will be passed to your method as parameters. 如果您的URI包含两个以上的段,它们将作为参数传递给您的方法。

From Codeigniter Controllers Guide Codeigniter控制器指南

You need to use 您需要使用

$this->input->get('data_id')

because you're passing data via GET method. 因为您要通过GET方法传递数据。

So your code should be: 因此,您的代码应为:

public function details(){

    echo $this->input->get('data_id');

    $data['details'] = $this->wapi_db->details($this->input->get('data_id'));

    $data['latest_news'] = $this->wapi_db->latest_news();


    $data['main_content'] = "category/details";

    $this->load->view('includes/template',$data);


}

Here the docs: http://www.codeigniter.com/user_guide/libraries/input.html 这里的文档: http : //www.codeigniter.com/user_guide/libraries/input.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM