Welcome Guest, Not a member yet? Register   Sign In
Is this proper use of mcv?
#1

[eluser]Unknown[/eluser]
This is some stupidly easy code but since I'm new to MCV, I just wanted to check if this is proper use of MCV before I write lots of lines. Thanks a lot

Model:
Code:
<?php
class Navigation_model extends Model {
        
    function Navigation_model()
        {
            // Call the Model constructor
            parent::Model();
        }

        function get_navigation()
        {
            $this->db->order_by("position", "asc");
            $query = $this->db->get_where('navigation', array('visible' => 1));
            
            return $query;
        }
}
/* End of file navigation_model.php */
/* Location: ./system/application/config/models/navigation_model.php */

Controller:
Code:
<?php
class Navigation extends Controller {

    function Navigation()
    {
        parent::Controller();
    }

    function index()
    {
        $this->load->model('Navigation_model');

        $data['navigation'] = $this->format_navigation();

        $this->load->view('profile', $data);
    }

    function format_navigation()
    {
        $query = $this->Navigation_model->get_navigation();
        
        $output = "<ul class=\"topnav\">";
        foreach ($query->result() as $row) {
            $output .= "<li>";
            $output .= "<a >url_name) . ".php". "\">{$row->menu_name}</a></li>";
        }
        $output .= "</ul>";
        return $output;
    }
}
/* End of file navigation.php */
/* Location: ./system/application/config/controller/navigation.php */

view:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to CodeIgniter&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h1>Just testing stuff</h1>
&lt;?php
   echo $navigation;
?&gt;

<p><br />Page rendered in {elapsed_time} seconds</p>

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]brianw1975[/eluser]
that's pretty much it.

some would go further and say to put the code that generates the navigation html into the view i.e: use php code in the view file that is basically the interior of the format function.

this is covered in the User Guide Views chapter
#3

[eluser]jdfwarrior[/eluser]
Looks good. Just as a hint though, and this isn't something thats required obviously, but longer named items such as your Navigation_model, you can get a simpler name by giving a second parameter when you load it.

Code:
$this->load->model('Navigation_model', 'nav');

Would allow you to access it via:

Code:
$this->nav->function()

Like I said, not something major, just something I like to do to save time from typing long names and keeps me from making so many dang typos by having to type the long names.
#4

[eluser]gyo[/eluser]
You could even put the navigation in a separate view file (to keep the main template clean).




Theme © iAndrew 2016 - Forum software by © MyBB