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 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流