Passing multiple values to view from controller |
[eluser]ross_c[/eluser]
Hi Ive been exploring codeigniter for a week or so now and am really enjoying using it. Previously I have had a windows .net background and have really wanted to move to open source solutions / community based technologies. I have been making a bit of a customer database for myself whilst learning how to use the codeigniter framework. I have come unstuck in this particular situation and may be going about it the wrong way. I have a controller that displays several views here is my controller code: Code: function view($id) the subContactList view contains contacts that have the same customer_id as the customer in the customerView. Basically my question is how do i also pass the $customer_id to the subContactList view as well as the $table variable so that i can use it on my subContactList view to create a new customer and link back using the id etc. hope that makes sense. If it helps below is the code from the getCustomerContacts function. Code: public function getCustomerContacts($customer_id) Hope my question makes sense, many thanks in advance. Ross
[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums! I'm glad you're enjoying CodeIgniter. All of the code below is untested, and may contain bugs, but I hope you might find it useful. Your model method: Code: public function getCustomerContacts($customer_id) Your controller method: Code: function view($id) I would also recommend the following: Check that a customer with the given ID exists, and react accordingly. At present, you're will get a load of errors if you use a customer ID that doesn't exist. Name your views consistently. You seem to have a mixture of camelcase, and lowercase separated with underscores. Consider moving your block of view-loading code into its own method. This means that you won't have to repeat yourself. It also means you can add some extra flexibility, such as being able to define which partial view to load, or perhaps to hide the header/footer if you wanted to. For example: Code: protected function _load_template($data = array(), $options = array()) Now, let's hide the header and footer, but load customerView: Code: $this->_load_template($data, array( What if we want to load multiple views within our template? Code: $this->_load_template($data, array( I know it looks like a lot of code, but it's very simple and gives you flexibility. You can add more logic to make it fit into your situation. Keep it as simple as possible, and add extra logic when you need a new feature. If you don't need to hide the header or footer, don't bother implementing the logic to do so. If you only need to load a single view within the template, then don't add the logic to support multiple sub-views being loaded. The advantage of this is that you save repeating yourself, and you have a central place to maintain for your view loading logic. If you want to reuse it, you can have a Customer_controller which you can put it into, and then extend that, so you don't have to keep repeating that code. I'm not sure which views are loaded every time, and which are the situational views, so I've just taken a guess just to illustrate what I'm trying to say. Hope this helps.
[eluser]ross_c[/eluser]
Thank you very much for the informative detailed reply. I will work through your recommendations and report back with my progress I appreciate the advice on improving the structure of my code and naming conventions too.
[eluser]ross_c[/eluser]
Just to give a bit of feed back. I have gone through my code and made the suggested changes I fully understand the benefits of what you suggested and think the loader method is great. I created a base controller called My_controller ( i followed this blog post guide here to create the base controller) and put the view loader method in this. Below is my base controller that my others are now extending from: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); I have also now gained a better understanding of passing arrays and passing arrays inside arrays to my views. Thanks again for your help much appreciated.
[eluser]TheFuzzy0ne[/eluser]
I'm glad you found it useful. Remember: If you find yourself repeating code, you probably just put it in the wrong place for the second time. Thanks for posting back. |
Welcome Guest, Not a member yet? Register Sign In |