Is it possible to do something like this in codeigniter:
publi function getcat($category_id)
{
$query = "(SELECT cat.id, cat.title ";
$query = $query . "FROM bf_categories cat ";
$query = $query . "WHERE cat.id=" . $category_id;
$this->db->limit(2, 4);
$records = $this->db->query($query);
return $records->result();
}
I've simplified the query just for demo purposes... but it's actually quite complex, which is why I've decided to use the query()
method.
But the limit clause is not being included in the query... I've verified by enabling the codeigniter profiler in my controller and I can see that the query is run with out any limit clause.
Can you tell me how I can accomplish this using the query()
method?
Edit 1
I've modified my model to look like this:
public function get_categories_and_products($limit=5, $offset=0, $category_id=null)
{
print "<BR>the function got the following offeset: $offset and limit: $limit";
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.") ?";
$records = $this->db->query($query,array($this->db->limit(2, 4)));
return $records->result();
}
I've hardcoded the limit values for now... but ultimately, i'll be using the values that get passed into the method. Unfortunately, this code still does not work. According to the profiler, here's what's being executed:
(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight FROM bf_categories cat WHERE cat.parent_id=3 AND cat.category_id <>3) UNION (SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight FROM bf_product p Inner join bf_product_category cp on p.product_id=cp.product_id Where cp.category_id=3)
SELECT * FROM (`bf_categories`) WHERE `category_id` = '3' LIMIT 4, 2
So it's creating two separate select statements.
In the end, I'm doing everything manually.
public function get_categories_and_products($limit=10, $offset=0, $category_id=null)
{
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.") limit ".$offset.','.$limit;
$catsandprods= $this->db->query($query);
return $catsandprods->result();
}