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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.