Jump to content

Can't include dynamic nav menu in MVC views


GrfxGuy79

Recommended Posts

I am building a site using PHP, OOP, and an MVC framework. i am not using laravel, codeignitor, or any other framework, I am building this on my own. I am a newbie to mvc and so I am learning how it all works.

I have a dynamic nav menu that populates the categories from the db. The menu/categories will populate and work fine when I view the nav page on its own, but it will not work when it is included in the index view page. What should I do to make the dynamic menu view on all the view pages that i need it to? 

Below is my code, any guidance would be greatly appreciated.

Controller:

class Behindthescenes extends Controller
{
    public function __construct()
    {
        $this->btsModel = $this->model('mBehindthescene');
    }

    public function index()
    {
        $data = [
            'title' => 'Admin Dashboard',
        ];
        $this->view('/behindthescenes/index', $data);
    }

// Get categories for nav menu
    public function bts_cat_nav()
    {
        $catnav = $this->btsModel->getCatsNav();
        $data = [
            'catnav' => $catnav,
        ];
        $this->view('/includes/bts_cat_nav', $data);
    }
}

Model:

class mBehindthescene
{
    private $db;

    public function __construct()
    {
        $this->db = new DBC;
    }

    // Get Categories for Admin Navigation
    public function getCatsNav()
    {
        $this->db->query('SELECT * FROM categories ORDER BY cat_name ASC');
        $result = $this->db->fetchMultiple();
        return $result;
    }
}

Index View:

require_once APPROOT . '/views/includes/header.php';
require_once APPROOT . '/views/includes/bts_nav.php';
require_once APPROOT . '/views/includes/bts_cat_nav.php';

?>

<div class="container-fluid">
  ADMIN DASHBOARD

</div>

Nav Menu View:

<div class="container">
  <div class="row">
    <div class="cat_nav">
      <ul>
        <?php foreach ($data['catnav'] as $cat): ?>

        <li class="nav-item"><a href="<?php echo $cat->cat_link; ?> class=" nav-link text-white p-1 mb-1
            sidebar-link"><i
              class="<?php echo $cat->cat_fa; ?> text-light fa-lg mr-3"></i><?php echo $cat->cat_name; ?></a></li>

        <?php endforeach;?>
      </ul>
    </div>
  </div>
</div>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.