dsbj66959 2011-11-29 05:11
浏览 135
已采纳

如何正确实现动态菜单导航?

I'm trying to implement a menu navigation and its content. I don't have any issues to get datas in Model.

My current problem is loading dynamically each item on right place. I've been stucked on that. :(

I want to be able to load each page content when the user clicks on each menu item.

Ex:

Folio -> site.com/index.php/className/method/1 (Get the ID=1)

About -> site.com/index.php/className/method/2 (Get the ID=2) ...

I'd appreciate if there's a way to optimize and simplify the structure.

Here's my code so far Controller:

public function index() {
    $data = $this->loadPage();
    $this->load->view('content', $data);
}

public function loadPage($categoryID=1) {
    $data['title'] = $this->tabPageData;
    $data['tabMenu'] = $this->model->getAllMenuItems(); 
    $data['tabPhotos'] = $this->model->getAllPicturesByCategory($categoryID, 'url, model');

    return $data;
}

In my Content View:

$this->load->view('top_header.php'); 
$this->load->view('menu.php');
$this->load->view('photos.php');
$this->load->view('footer.php'); 
 ...

 Menu View:
 <?php foreach ($tabMenu as $item) : ?>
  $url = "<ul><li><a href='loadPage/" . $item->cat_id . "'>" . strtoupper($item->name) . "</a></li></ul>";
  echo $url; 
  <?php endforeach; ?>
...

Photo View:
<ul>
<?php foreach ($tabPhotos as $item) : ?>
 <?php echo "<li><img src=". base_url() . "images/photo1/" . $item->url. "></li>"; ?>                   
<?php endforeach; ?>
</ul>
  • 写回答

1条回答 默认 最新

  • douyi4297 2011-11-29 08:34
    关注

    urlHave tried using routes?

    In your routes file add something like;

    $route['page/(:any)'] = "page/load_page/$1";
    

    And have a 'page' controller to do something like;

    public function load_page($cat_id) 
    {
        $data = $this->whatever_model->get_page_by_id($cat_id);
        $this->load->view('content', $data);
    }
    

    The urls you generate would be something like; http://www.mysite.com/page/1

    You could then go one step further by creating and storing a pretty url for each page, then doing a lookup based on that url instead of using a number. The url_title() function would help if you want to do this.

    public function load_page($pretty_url) 
    {
        $data = $this->whatever_model->get_page_by_id($pretty_url);
        $this->load->view('content', $data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)