qpixo
qpixo

Reputation: 31

How to proper implement dynamically menu navigation?

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>

Upvotes: 1

Views: 354

Answers (1)

Rooneyl
Rooneyl

Reputation: 7902

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);
}

Upvotes: 1

Related Questions