0

I didn't find a similar topic, so I'm asking:

How to get over the problem of repeating the same model queries for all my CodeIgniter controller functions? For my site, I have to build my header and footer upon nearly same repeating database queries like this:

class Main extends CI_Controller
       {
       public function aboutus()
    {
    $this->load->model("read_db");
    $commondata["title"] = "Company - ".lang("aboutus");
    $commondata["mainmenu"] = $this->read_db->db_mainmenu();
    $commondata["mainprodcat"] = $this->read_db->db_allprodmaincat();
    $commondata["bestselling"] = $this->read_db->db_bestselling();
    $commondata["brochures"] = $this->read_db->db_allbrochures();
    $this->load->view("headerview", $commondata);

    $contentdata["aboutus"] = $this->read_db->db_aboutus();
    $this->load->view("view_aboutus", $contentdata);

    $this->load->view("footerview");
    }

public function contact()
    {
    $this->load->model("read_db");
    $commondata["title"] = "Company - ".lang("contact");
    $commondata["mainmenu"] = $this->read_db->db_mainmenu();
    $commondata["mainprodcat"] = $this->read_db->db_allprodmaincat();
    $commondata["bestselling"] = $this->read_db->db_bestselling();
    $commondata["brochures"] = $this->read_db->db_allbrochures();
    $this->load->view("headerview", $commondata);

            $contentdata["aboutus"] = $this->read_db->db_contact();
    $this->load->view("contactview", $contentdata);

    $this->load->view("footerview");
    }

       *further functions like this*
       }

Is there any option to outsource the same repeating model calls to another function or file? Thanks so much for any suggestions.

3
  • Not a direct answer so I write it as comment, but you should think about caching this data. Instead of genrating the footer once per call, you could generate it onc per hour. Smarty has such cache handling, Memcache is made for it, and Redis is wellworth to be checked out. Commented Jun 2, 2013 at 0:33
  • @ZsoltSzilagy CodeIgniter has its own cache handling and it's pretty good. Commented Jun 2, 2013 at 2:51
  • @ZsoltSzilagy Köszi Zsolt! That's a great idea, but I suppose I still have to generate the page the 1st time it is called even if it will be cashed. Commented Jun 2, 2013 at 12:30

1 Answer 1

1

You can make a group function in your model and then call only that one from the controller. For example:

MODEL

function db_bundle() {
    $data = array();
    $data["mainmenu"] = $this->db_mainmenu();
    $data["mainprodcat"] = $this->db_allprodmaincat();
    $data["bestselling"] = $this->db_bestselling();
    $data["brochures"] = $this->db_allbrochures();
    return $data;
}

CONTROLLER

$commondata = $this->read_db->db_bundle();

Note, instead of bundle, you can group your call by the pages that need them, so you can have separate groups for Contact, About Us, etc... or you can make your bundle function accept parameters that will allow you to control which functions should and should not be called from within.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Shomz, I tried that before and it didn't work for some reason (I probably haven't declared $data as array). Now I tried it again with your code suggestion and it works great. Thx again!
You're welcome... It could be you forgot to remove read_db from the model calls. Don't forget to accept the answer if it helped you. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.