1

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 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm having problem in my Menu View Link doesn't work properly: <ul><li><a href=index.php/site_controller/loadPage/" . $item->cat_id . ">" . strtoupper($item->name) . "</a>
Define not working properly. Also I would recommend getting rid of the index page in your url, amend your config - $config['index_page'] = ''; and upload a relevant htaccess file
I'm testing this project in localhost, not sure if .htaccess will work. If I tried to set $config['index_page'] = "" with no success. My issue is this static link
I set in route.php $route['site_controller/(:num)'] = "site_controller/loadPage/$1"; Removed $config['index_page']= ""; Set in Menu View: <a href="site_controller/" . $item->cat_id . ">" ... This annyoning index.php still needs to define
My main issue is links while clicking on each menu option, links starting to collapse each other. Ex: In browser URL: localhost:9999/ci/dev/site_controller/1/site_controller/2 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.