Welcome Guest, Not a member yet? Register   Sign In
Acrive Record: Getting the row total then setting limit clause
#1

[eluser]EEssam[/eluser]
Hi,

My search model looks like the following:

Code:
function get()
    {
        if ($this->brandid)
        {
            $this->db->where('brandid', $this->brandid);
        }

        $this->db->select('*');

        $this->db->from('product');

        $this->row_count = $this->db->count_all_results();

        $this->db->limit($this->row_count, $this->offset);

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

        return $query;
    }

But it's not working. I need to set the limit clause because my search results are paginated.

Please help. Thanks.
#2

[eluser]philpalmieri[/eluser]
Hey,

Best way i have found is to just do two queries

Code:
function get()
{
if ($this->brandid)
{
   $this->db->where('brandid', $this->brandid);
  }
  //Select Just the count in 1 result
  $this->db->select('COUNT(*) as ttl');
  $total = $this->db->get('product')->row();
  $total = $total->ttl;
  
  //Your paginated/limit query
   if ($this->brandid)
   {
     $this->db->where('brandid', $this->brandid);
   }
   $this->db->limit($howManyYouWant, $offset);
   $query = $this->db->get('product');
    return $query;
}
#3

[eluser]philpalmieri[/eluser]
where did your question go, now im not sure if my reply answered it or not, but it may help you figure it out
#4

[eluser]Armchair Samurai[/eluser]
I'd do it this way.

Code:
function get($limit, $offset)
{
    $data = new stdClass;

    $this->db->start_cache();
        $this->db->from('product');

        if ($this->brandid)
            $this->db->where('brandid', $this->brandid);
    $this->db->stop_cache();

    $data->count = $this->db->count_all_results();

    if ($data->count == 0)
    {
        $data->product = array();
    }
    else
    {
        $this->db->limit($limit, $offset);

        $query = $this->db->get();
        $data->product = $query->result();
    }

    $this->db->flush_cache();
    return $data;
}




Theme © iAndrew 2016 - Forum software by © MyBB