Sasha
Sasha

Reputation: 8705

Codeigniter 2.1 - counting the array before get

I have this function:

   function gi_get_by($col,$id, $itd, $tbl, $limit = 10)
        {    
        $this->db->select('*');
        $this->db->from('global_info');
        $this->db->join($tbl, 'id_' . $tbl . ' = id_global_info');
        $this->db->where('info_type_id', $itd);
        if($col == 'date_created' || $col == 'tag') {$this->db->like($col, $id);}        
        else {$this->db->where($col, $id);}
        if($tbl == 'ad') :
        $this->db->order_by('paid', 'desc');
        endif;
        $this->db->order_by('date_created', 'desc');  
        $this->db->limit($limit, $this->uri->segment(2));
        $q = $this->db->get();        
        return $q = $q->result_array();   
        }

What I need is to count number of results before limit and to use them later in controller. I have an idea to duplicate this function without $limit but then it will be duplicating the same function. Is there another way to do this or I have to go with duplication?

Upvotes: 1

Views: 153

Answers (4)

lracicot
lracicot

Reputation: 364

You should use Active Record Cache:

http://codeigniter.com/user_guide/database/active_record.html#caching

function gi_get_by($col,$id, $itd, $tbl, $limit = 10)
{    
    // Start the cache
    $this->db->start_cache();

    $this->db->select('*');
    $this->db->from('global_info');
    $this->db->join($tbl, 'id_' . $tbl . ' = id_global_info');
    $this->db->where('info_type_id', $itd);
    if($col == 'date_created' || $col == 'tag') {$this->db->like($col, $id);}        
    else {$this->db->where($col, $id);}
    if($tbl == 'ad') :
    $this->db->order_by('paid', 'desc');
    endif;
    $this->db->order_by('date_created', 'desc');

    // Stop the cache
    $this->db->stop_cache();

    // Get the total
    $total = $this->db->count_all_results();

    // Now set the limit
    $this->db->limit($limit, $this->uri->segment(2));

    $q = $this->db->get();

    // Important! Clear the cache
    $this->db->flush_cache();

    return $q = $q->result_array();   
}

Upvotes: 0

dm03514
dm03514

Reputation: 55972

I don't quite understand what you want to do but if you want an optional limit you can default it to false:

 function gi_get_by($col,$id, $itd, $tbl, $limit=false)
        {    
        $this->db->select('*');
        $this->db->from('global_info');
        $this->db->join($tbl, 'id_' . $tbl . ' = id_global_info');
        $this->db->where('info_type_id', $itd);
        if($col == 'date_created' || $col == 'tag') {$this->db->like($col, $id);}        
        else {$this->db->where($col, $id);}
        if($tbl == 'ad') :
        $this->db->order_by('paid', 'desc');
        endif;
        $this->db->order_by('date_created', 'desc');
        if ($limit) {  
          $this->db->limit($limit, $this->uri->segment(2));
        }
        $q = $this->db->get();        
        return $q = $q->result_array();   
        }

This will conditionally add limit clause if it is passed in to the function.

Also it would be best to pass in wwhatever $this->uri->segment(2) into the function as a parameter instead of accessing it from within the function.

Upvotes: 2

manix
manix

Reputation: 14747

Well, how about something like this:

function gi_get_by($col,$id, $itd, $tbl, $limit = 10)
{
    $count = $this->db->query('SELECT * FROM my_table')->num_rows();

    //the rest stuff
}

Upvotes: 0

Related Questions