Jump to content

Math help


Kelset

Recommended Posts

Ok I have an array that I page through, I pass the current page throug the URL.

What I am having trouble with is finding what the last page would be. First I count the number of items in the array, I then
divied that by 10 (number of items showing per page) and round it. This method does not seem to be working, any ideas?

cheers!
Stephen
Link to comment
https://forums.phpfreaks.com/topic/26538-math-help/
Share on other sites

Remember that 10 items is still only 1 page.. subtract 1 from the number of items, and then your formula will work (or it may be off by 1, depending on if your pages start at page 1 or page 0).

This ought to work:

[code]$n = count($array) - 1;
$last_page = round($n / 10) + 1;[/code]

There's also some tutorials on pagination on this website somewhere..
Link to comment
https://forums.phpfreaks.com/topic/26538-math-help/#findComment-121408
Share on other sites

rounding the value may not work. Suppose there are 23 items in the array, then [i][b]round(23/10)[/b][/i] becomes [b]2[/b] and you add [b]1[/b] to this and get the number of pages i.e. [b]3[/b]. btherl's solution could give erronious result for a few values for e.g. if the No. of items is 29, then it will give 4 pages instead of 3. Here is the correct code that should work:

[code]$n = count($array);
$last_page = intval($n / 10) + 1;[/code]
Link to comment
https://forums.phpfreaks.com/topic/26538-math-help/#findComment-121419
Share on other sites

[quote]
rounding the value may not work. Suppose there are 23 items in the array, then round(23/10) becomes 2 and you add 1 to this and get the number of pages i.e. 3. btherl's solution could give erronious result for a few values for e.g. if the No. of items is 29, then it will give 4 pages instead of 3. Here is the correct code that should work:

[code]
$n = count($array);
$last_page = intval($n / 10) + 1;
[/code]
[/quote]

intval(...) internally calls round(...), so essentially its almost the same thing as above...

Putting the logic into a function can save repetition, (see below)

You'll want to know the [b]minimum[/b] number of pages required, therefore;
You need to round [b]up[/b] to the nearest whole number. For example if you have [b]23[/b] results, at [b]10[/b] results per page, and you use round, you going to [b]get (23/10) = round(2.3) = 2.0[/b]  that means 2 pages, but what about the [b]3[/b] more results ? Well your going to cut them off :)

And my v. simple function,

[code]function CalculateNumberOfPages($iTotalItems, $iItemsPerPage)
{
return celi($iTotalItems / $iItemsPerPage);
}

$last_page = CalculateNumberOfPages(count($array), 10);[/code]

Note: celi rounds up, so if the number is [color=blue](float)[/color] [b]2.3[/b], it becomes [color=blue](int)[/color] [b]3[/b].

Hth :)
Link to comment
https://forums.phpfreaks.com/topic/26538-math-help/#findComment-121437
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.