2013-02-22 61 views
0

我无法检索并将url中的用户标识放入变量中。这是我正在尝试使用的控制器。我已阅读用户指南中的文档,但我没有得到任何结果。

这里是我的URL结构:

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); 


    } 

我是全力以赴这样做不对,还是有我需要在我的配置文件来进行调整?

在此先感谢

+1

您正在寻找'$ _GET ['id']'或'$ _GET [$ user ['id']]'的id吗? – 2013-02-22 18:19:35

+0

对不起,我忘了添加网址结构。 http://www.clci.dev/account/profile/220。我想获得220.哪个是$ user ['id']; – LightningWrist 2013-02-22 18:20:50

+0

而GET中的哪个键值是220? 'var_dump($ _GET)' – 2013-02-22 18:21:32

回答

2

在笨,而不必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)

+0

看我的编辑。我已经实现了mod重写。 – LightningWrist 2013-02-22 18:35:56

2

你会设置你的控制器功能如下

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

你可以直接得到这样的:

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

其中是$ user_id创建?在那里你有它,或者我应该在其他地方宣布它? – LightningWrist 2013-02-25 17:36:41

+0

$ user_id不是在任何地方创建的......你调用了这个URL clci.dev/account/profile/220...so $ user_id的值是220的url值......因为你调用了控制器的配置文件功能 – 2013-02-26 05:18:21

0

我想在这一点上,对于$ _GET [“身份证”]的值是打算在220所以在这里: 要得到220,你必须这样做(除了获得的价值是220以外的地址如上面的url所示)

假设您访问: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); 


} 

我希望这可以帮助你开始正确的轨道。