如何将$ _GET值放在codeigniter中的变量中

时间:2013-02-22 18:16:17

标签: php codeigniter get

我无法检索并将url中的用户ID放入变量中。这是我的控制器,我正试图这样做。我已经阅读了用户指南中的文档,但是我没有得到任何结果。

这是我的网址结构:

clci.dev/account/profile/220

控制器:

public function profile() 
    {

        $this->load->helper('date');
        $this->load->library('session');
        $session_id = $this->session->userdata('id');
        $this->load->model('account_model');
        $user = $this->account_model->user();
        $data['user'] = $user;
        $data['session_id'] = $session_id;
        //TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
        $user_get = $this->input->get($user['id']); 
        echo $user_get;
        if($user['id'] == $session_id)
        {
            $data['profile_icon'] = 'edit';
        }
        else
        {
            $data['profile_icon'] = 'profile';
        }
        $data['main_content'] = 'account/profile';
        $this->load->view('includes/templates/profile_template', $data);


    }

我做错了,或者我需要在配置文件中进行调整吗?

提前致谢

4 个答案:

答案 0 :(得分:2)

在codeigniter中,我们使用something.com/user.php?id=2而不是something.com/user/2,而获取该代码2的方法是使用此方法:

$this->uri->segment(3)

了解更多信息http://ellislab.com/codeigniter/user-guide/libraries/uri.html

编辑:

根据您的网址:clci.dev/account/profile/220,您需要$this->uri->segment(4)

答案 1 :(得分:2)

您可以按如下方式设置控制器功能

public function profile($id = false) 
{
     // example: clci.dev/account/profile/222
     // $id is now 222
}

答案 2 :(得分:0)

你可以直接这样:

public function profile($user_id = 0) 
{
     //So as per your url... $user_id is 220
}

答案 3 :(得分:0)

我想在这一点上$ _GET ['id']的值应该是220,所以这里: 要获得220,你必须这样做(除了上面的网址中显示的有问题的获取值不是220)

假设您访问:clci.dev/account/profile/220。请按照评论获取更多信息。

public function profile() 
{
    $this->load->helper('url'); //Include this line
    $this->load->helper('date');
    $this->load->library('session');
    $session_id = $this->session->userdata('id'); //Ensure that this session is valid
    $this->load->model('account_model');
    $user = $this->account_model->user(); //(suggestion) you want to pass the id here to filter your record
    $data['user'] = $user;
    $data['session_id'] = $session_id;
    //TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
    $user_get = $this->uri->segment(3); //Modify this line
    echo $user_get; //This should echo 220
    if($user_get == $session_id) //Modify this line also
    {
        $data['profile_icon'] = 'edit';
    }
    else
    {
        $data['profile_icon'] = 'profile';
    }
    $data['main_content'] = 'account/profile';
    $this->load->view('includes/templates/profile_template', $data);


}

我希望这有助于您开始正确的方向。