1

I have a question. I have a function in a model to select products:

public function getProducts()
{
    $this->db->select(dbColumnProductsId . ',' . dbColumnProductsName . ',' . dbColumnProductsImage . ',' . dbColumnProductsPrice . ',' . dbColumnProductsStock . ',' . dbColumnCategoryId . ',' . dbColumnCategoryName);
    $this->db->join(dbTableCategory, dbTableCategory . '.' . dbColumnCategoryId . '=' . dbTableProducts . '.' . dbColumnProductsCategory);
    $this->db->order_by(dbColumnProductsId, 'DESC');
    $this->db->from(dbTableProducts);
    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        return $query->result_array();
    }

    return FALSE;
}

But now I need to make a pagination of these products. How did I can use the "num_rows" function in the same function? This way there would have to create a separate function. Sorry for my poor english

2 Answers 2

1

You can return both adding them into a array like

public function getProducts()
{
    $this->db->select(dbColumnProductsId . ',' . dbColumnProductsName . ',' . dbColumnProductsImage . ',' . dbColumnProductsPrice . ',' . dbColumnProductsStock . ',' . dbColumnCategoryId . ',' . dbColumnCategoryName);
    $this->db->join(dbTableCategory, dbTableCategory . '.' . dbColumnCategoryId . '=' . dbTableProducts . '.' . dbColumnProductsCategory);
    $this->db->order_by(dbColumnProductsId, 'DESC');
    $this->db->from(dbTableProducts);
    $query = $this->db->get();

    $retun_array = array();
    $retun_array['rows'] = $query->num_rows();
    if($retun_array['rows'] > 0)
    {
         $retun_array['data']= $query->result_array();
    }

    return $retun_array;
}

And you can use this in you controller after getting model return for example if you store return data in $retun_array then

$rows = $retun_array['rows'];
if($rows > 0){
    $data = $retun_array['data']
}
Sign up to request clarification or add additional context in comments.

Comments

0

To paging must through three stages to handle:

  • Count products, you can use $db->count_all_results(); function
  • Find the limit and start
  • Select products by limit and start

dont use num_rows because its too slow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.