drtoaamk20278
2016-01-23 02:26
浏览 36
已采纳

如何通过将id从页面链接传递给控制器​​来获取当前页面的内容

I am working in Codeigniter.

Here is my code which I have written so far to achieve what I am thinking.

View:

<li><a href=" echo $menu->url.'/'.$menu->id " >Gallery</a></li>

Controller:

function view($id){

$content['content'] = $this->MyModel->getContent($id);
$data['header'] = $this->load->view('header', '', true);
$data['content'] = $this->load->view('content', $content, true);
$this->load->view('main_view', $data);

}

Model

 function getContent($id){

$this->db->select('page_title, content');
$this->db->from('admin_pages');
$this->db->where('id', $id);
............
return $query;
}

Route

$route['view/(:any)'] = 'mycontroller/view/$1'; 

What I want to achieve here is, when I click the link in the "View" then it should show me another page with the content of that specific link fetched from the "admin_pages" table based on the parameter "$id" passed to it from the url.

However when I click the link it gives me page not found error.

Any kind of help will be much appreciated.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutou3725 2016-01-23 03:06
    已采纳

    After trial and error, I found a workaround which I want to share for future references if made.

    I changed the link as:
    View

    <li><a href=" echo base_url().'page/'. $menu->id; " >Gallery</a></li>
    

    Then in the route I changed:
    Route

    $route['page/(:any)'] = 'mycontroller/view/$1'; 
    

    What this means is, whenever there in the URL is base_url/page followed by an "id" then it will be remapped or taken to the "view" method in the controller "mycontroller" and the id will be passed as a parameter to the method, which the method will recieve as $id in its definition.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题