Welcome Guest, Not a member yet? Register   Sign In
Active Record and Arrays
#1

[eluser]bagwaa[/eluser]
Howdi,

I am pretty new to CI and I am trying to use active record rather than generic PHP where I can. Basically my problem is as follows, I am writing a job costing application, one of the aspects of this app is that the user can create a job, and then create a bunch of costs specific to that job, and then he can apply those costs to that job, so its pretty simple.

One of the things I am trying todo is create a function within my model to pull back only the costs from the database which are active, I have an array of cost id's and I have access to the cost table, so I need to work these two things together and use active record to only pull out details from the database where the id field is equal to what is in my array, I thought something like this would work.

Code:
function getAppliedCosts($job_id) {
        
        $active_costs_array        = $this->_fetchAppliedCostsArray($job_id);

        $this->db->order_by('id', "asc");
        
        foreach($active_costs_array as $active_cost) {
            $this->db->where('id', $active_cost);
        }
        
        $query    = $this->db->get('mast_costs');
        $costs     = $query->result();
        
        return $costs;

        }

Does anyone know how I can use active record to pull out data from a table where the id is equal to each element of an array?

Hope that makes sense, thanks,

Richard
#2

[eluser]bagwaa[/eluser]
It seems I may have fixed my own problem with where_in() this code below seems to work.

Code:
function getAppliedCosts($job_id) {
        
        $active_costs_array        = $this->_fetchAppliedCostsArray($job_id);

        $this->db->order_by('id', "asc");
        
        // pull back only the indexes in the array from the table
        $this->db->where_in('id', $active_costs_array);
        
        $query    = $this->db->get('mast_costs');
        $costs     = $query->result();
        
        return $costs;

    }
#3

[eluser]TheFuzzy0ne[/eluser]
You can go one better than that, and lose the foreach loop:

Code:
function getAppliedCosts($job_id) {
        
    $active_costs_array = $this->_fetchAppliedCostsArray($job_id);

    $this->db->order_by('id', "asc");
        
    $this->db->where_in('id', $active_cost);
        
    $query = $this->db->get('mast_costs');
    $costs = $query->result();
        
    return $costs;

}

EDIT: D'oh! It's bad enough being beaten by Dam1an, but now you want a piece of the action! Tongue
#4

[eluser]bagwaa[/eluser]
haha thanks for the reply though :-)

Richard
#5

[eluser]Dam1an[/eluser]
[quote author="TheFuzzy0ne" date="1246551515"]
EDIT: D'oh! It's bad enough being beaten by Dam1an, but now you want a piece of the action! Tongue[/quote]

Haha! Love it Smile
You treat it like some sort of competition
#6

[eluser]TheFuzzy0ne[/eluser]
No, not at all. I'm just a sarcastic (insert obscenity here).




Theme © iAndrew 2016 - Forum software by © MyBB