CodeIgniter Forums
Acrive Record: Getting the row total then setting limit clause - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Acrive Record: Getting the row total then setting limit clause (/showthread.php?tid=13022)



Acrive Record: Getting the row total then setting limit clause - El Forum - 11-07-2008

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


Acrive Record: Getting the row total then setting limit clause - El Forum - 11-07-2008

[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;
}



Acrive Record: Getting the row total then setting limit clause - El Forum - 11-07-2008

[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


Acrive Record: Getting the row total then setting limit clause - El Forum - 11-07-2008

[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;
}