Kelset Posted November 8, 2006 Share Posted November 8, 2006 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 thendivied 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 More sharing options...
btherl Posted November 8, 2006 Share Posted November 8, 2006 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 More sharing options...
Vikas Jayna Posted November 8, 2006 Share Posted November 8, 2006 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 More sharing options...
heckenschutze Posted November 8, 2006 Share Posted November 8, 2006 [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 More sharing options...
Vikas Jayna Posted November 8, 2006 Share Posted November 8, 2006 intval is different from round in the following way:-intval(2.8) gives 2round(2.8) gives 3 Link to comment https://forums.phpfreaks.com/topic/26538-math-help/#findComment-121448 Share on other sites More sharing options...
Kelset Posted November 8, 2006 Author Share Posted November 8, 2006 Thanks for the information guys but when I run the fucntion I get an error.Call to undefined function celi()any idea why I am getting this?I figured it out should be ceil() Stephen Link to comment https://forums.phpfreaks.com/topic/26538-math-help/#findComment-121592 Share on other sites More sharing options...
Kelset Posted November 8, 2006 Author Share Posted November 8, 2006 Works perfectly thanks for all your guys help, I am once again in debt to you have phpfreaks help pages :)I hope one day to return the favor!Cheers!Stephen Link to comment https://forums.phpfreaks.com/topic/26538-math-help/#findComment-121615 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.