Welcome Guest, Not a member yet? Register   Sign In
Array Handling vs. DB Query in a View
#1

[eluser]gscharlemann[/eluser]
I have an two dimensional array retrieved from a query:

Code:
[0] => Array
        (
            [company_id] => 3
            [user_id] => 1
            [company_name] => Company 4
        )

    [1] => Array
        (
            [company_id] => 1
            [user_id] => 1
            [company_name] => My Comapny, Inc

        )

    [2] => Array
        (
            [company_id] => 2
            [company_name] => test
            [user_id] => 1
        )

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.
#2

[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
get_company_info();

Code:
// in the helper

function get_company_info($array)
{
    $ci =&get;_instance();

    $query = $ci->company_model->get_data();
    
    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $arr)
        {
           // do stuff;
        }
    }
}
untested code but hope that helps.
#3

[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)
{
    foreach($company_array as $company)
    {
        if($company['company_id']) ==$contact['company_id'])
        {
            $contact[company_name] = $contact['company_name'];
        }
    }
}
This is fine for some entries, but if You intend to have large arrays - especially companies - this will be slow.

For larger arrays You'd better start a new query - write a dedicated method in the model - and call it from the view.
#4

[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
#5

[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 {

    function Company_helper() {
        $this->load->model('Company_model');
    }

    function __construct() {
        $this->load->model('Company_model');
    }

    function get_company($company_id) {
        $this->Company_model->get_company($company_id);
    }
}

In the view I have tried:
Code:
get_company($company_id);
and
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?
#6

[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 ( ! function_exists('get_company'))
{
    function get_company($company_id) {

         // fetch CI instance
        $CI =& get_instance();

        return $CI->Company_model->get_company($company_id);
    }
}

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.
#7

[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 controller? using $this-> works.

In the models I've been using (which works):
Code:
$this->CI->db->query(...);
I set $this->CI =& get_instance(); in the constructor of the model classes (but not in the controller classes).

Still learning the ropes of CI, appreciate your help with the "dumb" questions.
#8

[eluser]tomcode[/eluser]
Code:
$this
It is the PHP way to name the current object when in OO environment. It is not a variable but a reserved (expression ? word ? ). Any time You create a class, You can (must) refer by calling this to the class object.

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();
I assign a local reference (reference ==the & sign) to the Controller Class Object and can this way use its variables and methods.

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




Theme © iAndrew 2016 - Forum software by © MyBB