Most Appropriate way to Achieve... |
[eluser]JamesTaylor[/eluser]
What is the best way to achieve the following? I'm working on a site for a small chain of pubs / restuarants - 3 in total. The site carries information regarding the main company who own the 3 pubs and also shows information specific to each of the pubs. Within the layout of the pages there is a common area which displays up coming events across the pubs... there is only ever going to be 3 items displayed... but dependent upon which page is being viewed the 3 items will alter slightly. For example: On the 'about us' page which is dealing with the parent company it needs to show the next event at each of the pubs - 3 in total - 1 for pub A, 1 for pub B, 1 for pub C. But, on the pages which are specific to the individual pub it needs to show the next 3 at that pub - 3 for pub A What i don't want to do is duplicate the code across multiple controllers so it is tailored to every page as it seems long winded. I am thinking of extending the controllers by creating MY_Controller, then grabbing the next 3 events for each pub and passing it to the page. I would then create 4 views which could be loaded into the main template dependent upon which page of the site is being viewed. The 4 views would be: 1 for the next event at each pub; 1 showing next 3 events at pub A; 1 showing next 3 events at pub B; 1 showing next 3 events at pub C. However the problem i have with the above is that i am always going to be executing extra database requests in relation to the info i need - this doesn't seem like good pratice to me! When i show the next 3 events at pub A there will still be database calls being made for the next events at pub B and C but this info won't be used? It seems to me that if the info to be displayed was always the same breakdown, i.e just the next event at each pub extending the controller would be the way to go as i'd only have to do the code once and it'd be available to each page. But is there an appropriate way to achieve a similar efficence but with the variations taken into account? Sorry its a bit long winded but i want to learn the correct way of doing things! Thanks, James
[eluser]heavenquake[/eluser]
Create a model ( http://ellislab.com/codeigniter/user-gui...odels.html ) with some functions for getting data from pubs, and then push them to your views. for example: Controller: Code: class Pub extends Controller { Model: Code: class Pub_model extends Model { That's what I would do.
[eluser]JamesTaylor[/eluser]
Thanks heavenquake, I haven't done exactly as you outlined but your suggested methodology got me moving... i've created MY_Controller to extend all other controllers: Code: class MY_Controller extends Controller { my model is then Code: <?php and then i have created a view which i will load into the templete on every page Code: <?php if (isset($NextEvents_Stan)) This seems to work as i want, although i think i will end up having to explictly say that if $pub_id doesn't equal any of the names do the following rather than just having it as if not set because eventuall i imagine that uri segment 2 is going to be needed else where also. Thanks James |
Welcome Guest, Not a member yet? Register Sign In |