0

我在检索和将 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

4 回答 4

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)

于 2013-02-22T18:24:22.820 回答
2

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

public function profile($id = false) 
{
     // example: clci.dev/account/profile/222
     // $id is now 222
}
于 2013-02-22T18:38:02.587 回答
0

你可以直接得到这样的:

public function profile($user_id = 0) 
{
     //So as per your url... $user_id is 220
}
于 2013-02-23T05:03:11.273 回答
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);


}

我希望这可以帮助您在正确的轨道上起步。

于 2013-02-25T08:45:33.047 回答