Welcome Guest, Not a member yet? Register   Sign In
Question about dynamic menu loaded in global layout
#1

[eluser]basterz[/eluser]
Hello everyone I am a newbie in programing (started a few month ago) I choose codeigniter because it is very cool and readable. I want to ask how can I make my dynamic_menu visible into the global template.
This is my controller:
Code:
<?php
class dynamic_menu extends Controller

{

    function index()
    {
      $this->load->model('dynamic_menu_model');
      $data['menu'] = $this->dynamic_menu_model->getAll();
      $this->load->view('menu', $data, true);

    }
}
?>
This is my model:
Code:
<?php
class dynamic_menu_model extends Model
{
    function getAll()
    {
        $query = $this->db->get('dynamic_menu');
        return $query;
        
        if($q->num_rows() > 0)
        {
             foreach($q->result() as $row)
             {
                 $data[]=$row;
             }
             return $data;
        }
    }
}
?>
This is my view for the menu:
Code:
<?php
$q = $this->db->get('dynamic_menu');

if($q->num_rows() > 0)
{
    foreach($q->result() as $row)
    {
       $menu[]=$row;
    }
}
?>

      <div id="logo">
        <h1>Architecture Bureau</h1>
        <p>It's all possible</p>
      </div>
      <div id="nav">
        <ul>
            &lt;?php foreach($menu as $m): ?&gt;
          <li><a >url; ?&gt;">&lt;?php echo $m->name; ?&gt;</a></li>
            &lt;?php endforeach; ?&gt;
        </ul>
      </div>
This is my global layout:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;

    &lt;title&gt;Architecture&lt;/title&gt;
        &lt;link rel="stylesheet" href="&lt;?php echo base_url(); ?&gt;stylesheet/style.css" type="text/css" charset="utf-8" /&gt;

&lt;/head&gt;

&lt;body&gt;
  <div id="wrapper">
    <div id="header"> </div>
    <div id="left">
        &lt;?php
        
        $this->load->view('menu');
        
        ?&gt;
    </div>
    <div id="right">

    </div>
    <div class="clear"> </div>
    <div id="spacer"> </div>
    <div id="footer">
      <div id="copyright">
        Copyright &copy; 2007 Company Name All right reserved.
      </div>
      <div id="footerline"></div>
    </div>

  </div>
&lt;/body&gt;
&lt;/html&gt;
this is my part of the database that I think you may need:
Code:
CREATE TABLE IF NOT EXISTS `dynamic_menu` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `description` text NOT NULL,
  `meta_tags` text NOT NULL,
  `page_title` text NOT NULL,
  `url` text NOT NULL,
  `created_on` datetime NOT NULL,
  `uploaded_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `dynamic_menu`
--

INSERT INTO `dynamic_menu` (`id`, `name`, `description`, `meta_tags`, `page_title`, `url`, `created_on`, `uploaded_on`) VALUES
(2, 'Home', 'This is a homepage', 'some metas', 'Home', 'DynamicMenu', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(3, 'Recent Projects', 'Those are our recent projects', 'some metas', 'Our recent projects', 'Products', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(4, 'Services', 'This is service description', 'some metas', 'This is service title', 'Services', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(5, 'Clients', 'This is a client description', 'some metas', 'this is Client title', 'Clients', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(6, 'Solutions', 'Description for solutions', 'some metas', 'Title for solutions', 'redirect to solutions'
So for the moment I've hardcoded the query into the "menu" view but I know it is wrong. I am looking for some other solution.

Regards
#2

[eluser]Crimp[/eluser]
Unless you plan on making the menu part of a larger CMS-type system, I would not put things like menu items in a database. Database calls are "expensive" in terms of the resources a site consumes and are best kept to an absolute minimum.

When someone refers to a dynamic menu, they usually mean that the menu changes with the context - the menu is different for different pages. One of the easiest ways to achieve this globally is to make one view PHP file to be included as a partial view, like you do. Using the URI, see CI user guide, you can then swap the meny around.

The "menu" you have in the database above seem to include information that would belong to other tables. Read up on relational database design, normalization and use of foreign keys before building something too large.
#3

[eluser]jedd[/eluser]
Hi basterz and welcome to the CI forums.

First observation - in your model you may want to use [url="/user_guide/database/results.html"]result_array()[/url] rather than calling result() and then converting to an array.

Your dynamic_menu appears to already be visible in your global template - can you explain a bit more about what your problem is here (so far it looks pretty fine for someone who's just started out programming!)

If you mean that you want to have this menu available in every controller .. check out the wiki's entry on [url="/wiki/MY_Controller"]extending the core Controller class[/url] - this might be what you're looking for.
#4

[eluser]basterz[/eluser]
Hi and thank you for the replays Smile My dynamic_menu is visible because the query is hard coded into the menu view. For example I will have five pages 1 Home 2 About us 3 Services 4 News 5 Contacts and I want the menu to be visible in all pages. But for the moment I figure it out only with hard coding the query into the "menu" view




Theme © iAndrew 2016 - Forum software by © MyBB