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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效