• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
menu->submenu out of database

#1
[eluser]Bartolo![/eluser]
Hi guys!

this is my first post here and i want to let everybody know that codeigniter rocks! ...but you already knew that ;-)

okay...my problem:

i got a dropdown menu that comes out of the database, i got it working, but as you can see there is an function inside the view. thats off course not what i want.

is there anyone who can help me rebuild this so i have the functions on the place where they belong?

Code:
TABLE 'insurance_categorie'
- cat_id
- title
- info


Code:
TABLE 'insurance_item'
- insurance_id
- cat_id
- title
- link
- info

Model
Code:
function getMenuCategories() {
    $query = $this->db->get('insurance_categorie');
    if($query->num_rows() > 0){
        return $query->result_array();
    } else {
        show_error('Fout! Geen menu items opgehaald.');
    }  
}

function getMenuItems($cat){
    $this->db->select('*');
    $this->db->from('insurance_item');
    $this->db->where('cat_id', $cat);
    $query = $this->db->get();
    if($query->num_rows() > 0){
        return $query->result_array();
    } else {
        show_error('Fout! Geen menu items opgehaald.');
    }        
}

Controller
Code:
$data['menu_categories'] =  $this->Mainmodel->getMenuCategories();

View
Code:
foreach($menu_categories as $menu_categorie):
    echo "<li class=\"level1-li\"><a >" . $menu_categorie['title'] . "</a>";
    echo "<ul class=\"sub\">";
    foreach($this->Mainmodel->getMenuItems($menu_categorie['cat_id']) as $submenuitem):
        echo "<li><a >" . $submenuitem['title'] . "</a></li>";
    endforeach;
    echo "</ul>";
echo "</li>";
endforeach;

#2
[eluser]Ben Edmunds[/eluser]
Modifying your controller to something like this should work for you:


Code:
$data['menu_categories'] =  $this->Mainmodel->getMenuCategories();

foreach ($data['menu_categories'] as $key => $category) {
   $data['menu_categories'][$key]['submenu'] = $this->Mainmodel->getMenuItems($category['id']);
}

#3
[eluser]slowgary[/eluser]
Queries in a loop are generally not a good idea. If you end up with 50 categories, each page load is going to be running 51 queries just to show this navigation, that won't include any other queries you need for your app. The same data can be pulled out of the database with just a single query, much more efficient. The idea would be to get the data, assemble it in your controller, then pass it to the view for display.

Something like this:
Code:
SELECT *
   FROM insurance_category
   JOIN insurance_item
      ON insurance_category.id = insurance_item.cat_id
   ORDER BY insurance_category.title, insurance_item.title

#4
[eluser]Bartolo![/eluser]
Hi guys, thank you both for your answering.

@Ben: i'm sorry but i don't understand your construction and i dont got it working.

@slowgary: that was my first plan, i did had a join but didnt get the loop in the view properly working. if you have an axample for me i would really appreciate it.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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