Array Handling vs. DB Query in a View |
[eluser]gscharlemann[/eluser]
I have an two dimensional array retrieved from a query: Code: [0] => Array I also have a second array of contacts that have a company_id. As I cycle through the contact array, I would like to use the company_id (from the contact_array) to get the company_name from the two-dimensional (company) array shown above. I'm not sure if or how to do that. I could run a db->query in the view to get the company_name from the company_id, but the data is in the view (I just can't get to it) and that's probably a huge MVC sin. Thanks for reading this.
[eluser]Thorpe Obazee[/eluser]
If you go with your first option, you could create a helper for this and output from there. eg. Code: // in the view Code: // in the helper
[eluser]tomcode[/eluser]
If You want to match the company_id from the contact_array by cycling the existing data : Code: foreach($contact_array as &$contact) For larger arrays You'd better start a new query - write a dedicated method in the model - and call it from the view.
[eluser]gscharlemann[/eluser]
Thank you both for your feedback. I guess i didn't realize I could setup a helper and call it from the view. That looks to be the cleanest (and most scalable) approach. Cheers
[eluser]gscharlemann[/eluser]
For some reason, I'm having trouble loading the helper I've created. I setup a file: company_helper.php. It's located in the helper folder. Code: class Company_helper { In the view I have tried: Code: get_company($company_id); Code: $this->Company_helper->get_company($company_id); Neither worked. I tried adding 'Company_helper' to the autoload.php config file... no dice. This is the error that I'm getting: Message: Undefined property: CI_Loader::$Company_helper Where did I go wrong?
[eluser]tomcode[/eluser]
A helper is NOT a class, just a collection of functions, which, once loaded, are overall available. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); If You call the function only in one view, You can call the model right from there, no need to have an extra helper. Writing the helper makes only sense if You need this functionality in several places, especially where You do NOT have the model already loaded.
[eluser]gscharlemann[/eluser]
Thanks Tom. I just used the code you provided, but changed it in the view to call the model directly. If I need to use this function in another file, I'll setup a helper as you advise. Thanks for setting me straight. One more question. When do I use $CI->... vs. $this->? For example, in my controllers, I've been using: Code: $this->Company_model->get_company(...); Should I be using Code: $CI->Company_model->get_company(...) In the models I've been using (which works): Code: $this->CI->db->query(...); Still learning the ropes of CI, appreciate your help with the "dumb" questions.
[eluser]tomcode[/eluser]
Code: $this See the PHP manual Classes and Objects, The Basics To allow to reach the Controller Object from outside, CodeIgniter offers the function get_instance(). With Code: $CI =& get_instance(); In view files under certain circumstances $CI works too (at least older versions), don't know why, probably CodeIgniter internaly declares it somewhere on the way, it's maybe a PHP4/PHP5 thing. I personnaly try to avoid these workarounds, I try to build my apps with a clean object structure, but of course, a helper with access to the Controller is a powerful tool. Traditions Mainly used in libraries, which do not have access to the controller object, unlike the models who can use the in the controller declared variables and classes. Code: $CI // the CodeIgniter way of naming a local reference to the controller object Code: $this->CI // the CodeIgniter way of naming a class wide reference to the controller object |
Welcome Guest, Not a member yet? Register Sign In |