【问题标题】:How to put $_GET value in a variable in codeigniter如何将 $_GET 值放入 codeigniter 的变量中
【发布时间】:2013-02-22 18:16:17
【问题描述】:

我在检索 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);


    }

我是否完全做错了,或者我需要在我的配置文件中进行调整?

提前致谢

【问题讨论】:

  • 您在$_GET['id']$_GET[$user['id']] 中寻找的id 吗?
  • 对不起,我忘了添加 url 结构。 clci.dev/account/profile/220。我想得到 220。这是 $user['id'];
  • GET 中的哪个键是 220? var_dump($_GET)
  • 这些是我使用 var_dump 得到的结果:array(0) { }
  • 根据框架文档处理此问题的正确方法与 skrilled 解释的完全一样,您可以在控制器函数中声明变量。

标签: php codeigniter get


【解决方案1】:

在codeigniter中,我们使用something.com/user/2而不是something.com/user.php?id=2,而获得2的方法是使用这个:

$this->uri->segment(3)

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

编辑:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我想此时 $_GET['id'] 的值应该是 220,所以在这里: 要获得 220,您必须这样做 (除非有问题的 get 值不是 220,如您上面的 url 所示)

      假设您访问:clci.dev/account/profile/220。关注 cmets 了解更多信息。

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

      我希望这可以帮助您走上正轨。

      【讨论】:

        【解决方案4】:

        你可以直接这样得到:

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

        【讨论】:

        • $user_id 是在哪里创建的?就在你拥有它的地方,还是我应该在其他地方声明它?
        • $user_id 没有在任何地方创建...您调用了这个 url clci.dev/account/profile/220...所以 $user_id 的值是 url 值,例如 220...因为您调用了控制器的配置文件功能
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 2017-02-04
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 2019-07-10
        相关资源
        最近更新 更多