目前,我有一个类似于以下的页面-abc.com/controller/action/23
这里的23是项目ID,它是动态的。我必须使用ID从数据库中获取项目的名称,然后路由至新的url,例如以下- abc.com/controller/p/23-itemname
。我怎样才能做到这一点?
当用户访问此页面时,您可以做的是:
abc.com/controller/action/23
您将此代码放入控制器中
控制器
function action()
{
$id = $this->uri->segment(3); //id is now 23, this is the third uri
$data['details'] = $this->YOUR_MODEL->get_details($id);
foreach($data['details'] as $d)
{
$itemname = $d->YOUR_TABLE_COLUMN_NAME;
}
redirect(base_url(). 'controller/p/' . $id . '-' . $itemname);
//line produces abc.com/controller/p/23-itemname
}
模型
function get_details($id)
{
$this->db->select('*');
$this->db->from('YOUR_TABLE_NAME');
$this->db->where('YOUR_ID_COLUMN_NAME',$id);
$query = $this->db->get();
return $query->result();
}
如果您需要更多说明,请发表评论。
祝你好运
编辑
由于您有操作并且您重定向到函数p,因此将发生这种情况。
function p()
{
$explodethis = $this->uri->segment(3); //Contains "23-itemname"
$pieces = explode("-", $explodethis); //Separate by the dash
$id = $pieces[0]; //Contains "23"
$itemname = $pieces[1]; //Contains "itemname"
$data['details'] = $this->YOUR_MODEL->YOUR_FUNCTION(YOUR_PARAMETERS)
$this->load->view('YOUR_VIEW',$data);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句