activerecord - CodeIgniter: How to use the get() active record twice with previous active records? -
i have following function under models shown..
public function get_products($category, $brand, $filter, $page, $limit) { // output $output = ""; // offset $offset = $limit * ($page - 1); // query pagination $this->db->select("*"); $this->db->from("products"); $this->db->where("category", $category); if ($brand != "all") { $this->db->where("brand", $brand); } // first time use $query = $this->db->get(); $total_rows = $query->num_rows(); $this->db->limit($limit, $offset); switch ($filter) { case 'latest': $this->db->order_by("released", "asc"); break; case 'most-viewed': $this->db->order_by("views", "asc"); break; case 'price-low-to-high': $this->db->order_by("price", "desc"); break; case 'price-high-to-low': $this->db->order_by("price", "asc"); break; } //second time use $query = $this->db->get();
here want use select
, from
, where
2 this->db->get()
can see query ends after running first get()
. there way that? or i've write active records twice?
the reason i'm trying num_rows
before limiting pagination total_rows
config.
here how can it.
public function get_products($category, $brand, $filter, $page, $limit) { $output = ""; $offset = $limit * ($page - 1); $this->getquery($category , $brand); $query = $this->db->get(); $total_rows = $query->num_rows(); //call query again $this->getquery($category , $brand); /* time limit , order applied query */ $this->db->limit($limit, $offset); switch ($filter) { case 'latest': $this->db->order_by("released", "asc"); break; case 'most-viewed': $this->db->order_by("views", "asc"); break; case 'price-low-to-high': $this->db->order_by("price", "desc"); break; case 'price-high-to-low': $this->db->order_by("price", "asc"); break; } //run query $query2 = $this->db->get(); } /* generate query , not run */ function getquery($category , $brand){ $this->db->select("*"); $this->db->from("products"); $this->db->where("category", $category); if ($brand != "all") { $this->db->where("brand", $brand); } }
Comments
Post a Comment