Welcome Guest, Not a member yet? Register   Sign In
How to get total records from Pager
#1

Hello,

I have a Product model that I perform a where search and then paginate the results.
If I use the countAllResults, I get an incorrect number of records found.

Is there a way to count all records returned?
Reply
#2

i used this approach to count entries in a db:

Code:
$query = $builder->where('category',$category)->countAllResults();
CMS CI4     I use Arch Linux by the way 

Reply
#3

(09-22-2022, 10:46 AM)captain-sensible Wrote: i used this approach to  count entries in a db:

Code:
$query = $builder->where('category',$category)->countAllResults();

I am using the Codeigniter 4 Model, would this still work or do I have to change to the builder class?

Thanks
Reply
#4

(This post was last modified: 09-23-2022, 02:18 AM by captain-sensible.)

had a quick look through some previous code of mine and found this:

Code:
            
$handle= new PortfolioModel();
                        $mycount= $handle->isEmptyMens();
                        $count= $mycount["COUNT(*)"];
                        if($count ==0)

It was when i was playing with a web for my daughter, fashion web.

now i coudn't find the code for the method isEmptyMens(); its one of my model->method names and will most certainly contain
contain stand model methods for geting data , and in the docs

also what I can say is that
Code:
    $handle= new PortfolioModel();
//here i am instantiating a Model based on CI4 model, where you set allowed fields etc.
//$handle is my  none imagination way of  naming the instance
$mycount= $handle->isEmptyMens();
//this will be using  one of the ci4 methods documented in "modelling data" in the docs
//something probably like findAll() but don't quote me on that , i have a bad memory on which work with objects , those that might use arrays


    $count= $mycount["COUNT(*)"];

in the above i leverage what a CI4 model standard method , in the docs, (modelling data) to get what i want  

I use an sqlite3 database for all my web. That is my approach to getting an actual count from  a class->method() object.
Now it hints to me that I also could find a direct way of using CI4 model and one of its ready methods ,to a get a count THEN. There may very well be a straight forward
way now to use CodeIgniter Model to get a Count. But i've i have shown using some lateral thinking you can get a count using CodeIgniter Model class and its methods.
In later code i just thought horses fore courses and just switched to Query builder to get the job done.
CMS CI4     I use Arch Linux by the way 

Reply
#5

(This post was last modified: 09-23-2022, 03:28 AM by frocco.)

(09-23-2022, 02:13 AM)captain-sensible Wrote: had a quick look through some previous code of mine and found this:

Code:
$handle= new PortfolioModel();
$mycount= $handle->isEmptyMens();
$count= $mycount["COUNT(*)"];
if($count ==0)

It was when i was playing with a web for my daughter, fashion web.

now i coudn't  find the code for the method isEmptyMens();  its one of my model->method names and will most certainly contain
contain stand model methods for geting data , and in the docs

also  what I can say is that
Code:
$handle= new PortfolioModel();
//here i am instantiating a Model based on CI4 model, where you set allowed fields etc.
//$handle is my  none imagination way of  naming the instance
$mycount= $handle->isEmptyMens();
//this will be using  one of the ci4 methods documented in "modelling data" in the docs
//something probably like findAll() but don't quote me on that , i have a bad memory on which work with objects , those that might use arrays


$count= $mycount["COUNT(*)"];

in the above i leverage what a CI4 model standard method , in the docs, (modelling data) to get what i want 

I use an sqlite3 database for all my web. That is my approach to getting an actual count from  a class->method() object.
Now it hints to me that I also could find a direct way of using CI4 model and one of its ready methods ,to a get a count THEN. There may very well be a straight forward
way now to use CodeIgniter Model to get a Count. But i've i have shown using some lateral thinking you can get a count using CodeIgniter Model class and its methods.
In later code i just thought horses fore courses and just switched to Query builder to get the job done.

Thanks for getting back to me.
The only way I found is to use two result sets based on same filtered query.

$result->like('cat_title', trim($locate) . '%');
$result2->like('cat_title', trim($locate) . '%');
$results2 = $result2->countAllResults();
$results = $result->paginate(5);
Reply
#6

I just realized that $this->model->pager->getTotal() returns the correct count.
duh
Reply




Theme © iAndrew 2016 - Forum software by © MyBB