Php 如何在codeigniter中的变量中放入$\u GET值
检索url中的用户id并将其放入变量时遇到问题。这是我的控制器,我正试图这样做。我已经阅读了用户指南中的文档,但是没有得到任何结果 以下是我的url结构:Php 如何在codeigniter中的变量中放入$\u GET值,php,codeigniter,get,Php,Codeigniter,Get,检索url中的用户id并将其放入变量时遇到问题。这是我的控制器,我正试图这样做。我已经阅读了用户指南中的文档,但是没有得到任何结果 以下是我的url结构: clci.dev/account/profile/220 控制器: public function profile() { $this->load->helper('date'); $this->load->library('session'); $sess
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);
}
我到底是做错了,还是需要在配置文件中进行调整
提前感谢codeigniter中的,我们使用
something.com/user.php?id=2
而不是something.com/user/2
,获得2的方法是:
$this->uri->segment(3)
更多信息
编辑:
根据您的url:clci.dev/account/profile/220,您需要在codeigniter中使用
$this->uri->segment(4)
,而不是使用something.com/user.php?id=2
我们使用something.com/user/2
,获得2的方法是:
$this->uri->segment(3)
更多信息
编辑:
根据您的url:clci.dev/account/profile/220,您需要
$this->uri->segment(4)
设置控制器功能,如下所示
public function profile($id = false)
{
// example: clci.dev/account/profile/222
// $id is now 222
}
您可以按如下方式设置控制器功能
public function profile($id = false)
{
// example: clci.dev/account/profile/222
// $id is now 222
}
你可以直接得到这样的结果:
public function profile($user_id = 0)
{
//So as per your url... $user_id is 220
}
你可以直接得到这样的结果:
public function profile($user_id = 0)
{
//So as per your url... $user_id is 220
}
我假设此时$_GET['id']的值为220,因此: 要获得220,您必须这样做(除了上面url中显示的get值不是220) 假设您访问:clci.dev/account/profile/220。请按照评论了解更多信息
公共功能配置文件()
{
$this->load->helper('url');//包括此行
$this->load->helper('date');
$this->load->library('session');
$session_id=$this->session->userdata('id');//确保此会话有效
$this->load->model('account_model');
$user=$this->account_model->user();//(建议)您想在此处传递id以筛选记录吗
$data['user']=$user;
$data['session\u id']=$session\u id;
//尝试使用$\u GET值生成变量
$user_get=$this->uri->segment(3);//修改此行
echo$user_get;//这应该是echo 220
if($user\u get==$session\u id)//也修改此行
{
$data['profile_icon']='edit';
}
其他的
{
$data['profile_icon']='profile';
}
$data['main_content']='account/profile';
$this->load->view('includes/templates/profile\u template',$data);
}
我希望这有助于您在正确的轨道上起步。我认为此时$\u GET['id']的值应该是220,因此: 要获得220,您必须这样做(除了上面url中显示的get值不是220) 假设您访问:clci.dev/account/profile/220。请按照评论了解更多信息
公共功能配置文件()
{
$this->load->helper('url');//包括此行
$this->load->helper('date');
$this->load->library('session');
$session_id=$this->session->userdata('id');//确保此会话有效
$this->load->model('account_model');
$user=$this->account_model->user();//(建议)您想在此处传递id以筛选记录吗
$data['user']=$user;
$data['session\u id']=$session\u id;
//尝试使用$\u GET值生成变量
$user_get=$this->uri->segment(3);//修改此行
echo$user_get;//这应该是echo 220
if($user\u get==$session\u id)//也修改此行
{
$data['profile_icon']='edit';
}
其他的
{
$data['profile_icon']='profile';
}
$data['main_content']='account/profile';
$this->load->view('includes/templates/profile\u template',$data);
}
我希望这有助于您在正确的轨道上起步。是您在
$\u GET['id']
或$\u GET[$user['id']]]
中查找的id吗?很抱歉,我忘了添加url结构。我正在设法得到220英镑。即$user['id'];GET的哪个键是220var\u dump($\u-GET)
这些是我使用var\u-dump:array(0){}得到的结果。按照框架文档的正确方法是,正如skrilled所解释的,您应该在控制器函数中声明变量。您在$\u-GET['id']
或$\u-GET[$user['id']]中查找的是id吗
?很抱歉,我忘了添加url结构。我正在设法得到220英镑。即$user['id'];GET的哪个键是220var\u dump($\u GET)
这些是我使用var\u dump:array(0){}得到的结果。根据框架文档,实现这一点的正确方法正是skrilled所解释的,您应该在控制器函数中声明变量。$user\u id是在哪里创建的?就在那里,你有它还是我应该在其他地方声明它?$user\u id不是在任何地方创建的…你称这个url为clci.dev/account/profile/220…所以$user\u id的值是像220一样的url值…因为你调用了controller的profile函数$user\u id是在哪里创建的?就在那里,你有它还是我应该在其他地方声明它?$user\u id不是在任何地方创建的…你称这个url为clci.dev/account/profile/220…所以$user\u id的值是像220一样的url值…因为你调用了controller的profile函数