• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about the template logic (Phil's template library)

#11
[eluser]victorche[/eluser]
ah, no ... you don't understand me ... the method is called get_names()
It is very short one:
Code:
function get_names()
    {
        $query = $this->db->get('menu_items'); // Simple db table, containing only id and name of the link, e.g. 'music', 'fun', 'jokes' so the links will be like site.com/stuff/jokes, site.com/stuff/fun

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $names[] = array(
                    'name' => $row['name']
                );
            }
            return $names;
        }
    }
But this is in my header. And will be repeated on every page. That's why I don't want to have this little method in every model. Like blog_model, search_model... I don't want to repeat the code. And I've read and read ... As far as I know, I have to put it MY_Model.php just because it will be used everywhere. And yes, my actual model is MY_Model.php
see this:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller
{

    function MY_Controller()
    {
        parent::Controller();

        $boxes = $this->my_model->get_names();

        $data = array(
            'menu_links' => $links
        );

        $this->template->set_layout('page_tpl');
        $this->template->set_partial('header', 'header_tpl', $data, FALSE);
        $this->template->set_partial('footer', 'footer_tpl', FALSE);
    }

}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
And MY_Model.php:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model
{

    var $table = "";

    function My_Model()
    {
        parent::CI_Model();
        $this->load->database();
    }

    function get_names()
    {
        $query = $this->db->get('menu_items');

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $names[] = array(
                    'name' => $row['name']
                );
            }
            return $names;
        }
    }

}

/* End of file MY_Model.php */
/* Location: ./application/core/MY_Model.php */
And the view file header_tpl.php:
Code:
<div id="menu">
        <ul>
        {menu_links}
            <li><a href="/box/{name}/">@{name}</a></li>
        {/menu_links}
        </ul>
    </div>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.