bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

paginate result "count" type float -> int #1187

Closed olaulau closed 4 years ago

olaulau commented 4 years ago

as written in the doc , the paginate function returns some infos. all are of type int, whereas "count" is of type float. I know it the result of a divide, but it's rounded so it will never be a decimal. so I this it has to be an int, so that strict comparison with "pos" will work.

pauljherring commented 4 years ago

I know it the result of a divide, but it's rounded so it will never be a decimal.

That's not the reason.

The code:

   148          function paginate(
   149                  $pos=0,$size=10,$filter=NULL,array $options=NULL,$ttl=0,$bounce=TRUE) {
   150                  $total=$this->count($filter,$options,$ttl);
   151                  $count=ceil($total/$size);

The relevant function:

https://www.php.net/manual/en/function.ceil.php

value rounded up to the next highest integer. The return value of ceil() is still of type float as the value range of float is usually bigger than that of integer.

olaulau commented 4 years ago

correct ! but it is the result of $total/$size, which is <= $total ($size is >= 1 I suppose) and $total is an int ... so $count will never be greater that integer maximum.

pauljherring commented 4 years ago

correct ! but it is the result of $total/$size, which is ...

irrelevant. ceil() always returns a float.

Cast it yourself in your own code if it's a problem.

pauljherring commented 4 years ago

.. hang on - how are you using $count with pos() (aka current()) - that function doesn't use numbers, nor does it return the index.

$ cat /tmp/x.php
<?php

$total = 10;
$size = 5;
$count = ceil($total/$size);

$arr = ['one', 'two', 'three'];

$dummy = next($arr);

echo "pos()=".current($arr)."\n";
$ php -f /tmp/x.php
pos()=two
olaulau commented 4 years ago

correct ! but it is the result of $total/$size, which is ...

irrelevant. ceil() always returns a float.

Cast it yourself in your own code if it's a problem.

Of course I can cast, I just though it would be more consistent if all were integers.

olaulau commented 4 years ago

.. hang on - how are you using $count with pos() (aka current()) - that function doesn't use numbers, nor does it return the index.

$ cat /tmp/x.php
<?php

$total = 10;
$size = 5;
$count = ceil($total/$size);

$arr = ['one', 'two', 'three'];

$dummy = next($arr);

echo "pos()=".current($arr)."\n";
$ php -f /tmp/x.php
pos()=two

I was thinking of indices returned by paginate().

xfra35 commented 4 years ago

Fixed in https://github.com/bcosca/fatfree-core/commit/214cab50e4db56b7ef9327937c0bd421330f0686