0 like 0 dislike
4 views
Digging in one system and a little stunned from the amount of code which is doing a count of the number of pages(something like pagination). Tons of variables, checks.
I thought, what's so complicated about that, and wrote the implementation:
`<?php</li>/** * Description of algorithm: * The algorithm prints the number of pages, based on three constants: * 1. The number of items(total) * 2. The desired number of elements on the page * 3. The maximum number of additions(the situation when on the last page 1-2 item) */define('RESULTS_COUNT', 12349);define('RESULTS_PER_PAGE', 17);define('MAX_ADDITIONS', 7); \$results_per_page = RESULTS_PER_PAGE>0?RESULTS_PER_PAGE:1; // additional check for division by zero if(RESULTS_COUNT<\$results_per_page || RESULTS_COUNT==\$results_per_page){ exit('page 1 for '.RESULTS_COUNT.' results.');} \$exact_pages_count = RESULTS_COUNT / \$results_per_page;if (is_int(\$exact_pages_count)) { exit(\$exact_pages_count.' pages for '.RESULTS_COUNT.' results.');}else { \$rough_estimate_pages = intval(\$exact_pages_count); \$number_of_surplus=RESULTS_COUNT-(\$results_per_page*\$rough_estimate_pages); \$pages_of_surplus=\$number_of_surplus<=MAX_ADDITIONS?\$rough_estimate_pages:\$rough_estimate_pages++; exit(\$rough_estimate_pages.' pages for '.RESULTS_COUNT.' results.');}?>* This source code was highlighted with Source Code Highlighter.`

Perhaps you have a better solution, a decisive the same task? If so, it would be interesting to look at.
| 4 views

0 like 0 dislike
% — modulo
floor() — round down
\r
\$countPages = floor(RESULTS_COUNT/RESULTS_PER_PAGE);
\r
\$countPages++;
};
\r
return \$countPages;
by
0 like 0 dislike
something is not clear, you need to calculate how many pages is obtained the number of results for a given number of elements on the page?
by
0 like 0 dislike
``if(RESULTS_COUNT<\$results_per_page || RESULTS_COUNT==\$results_per_page)``

\r
O_o
\r
There is a comparison operator <=
by
0 like 0 dislike
Oh, just the day before had to solve this problem for Silverlight applications (C# language). Here's what happened:
\r
\r` public static IDictionary DivideItems(IEnumerable sourceItems, int itemsAtPage){var result = new Dictionary();var array = sourceItems.ToArray();var j = 0;var pageNumber = 1;while (j < array.Length){var ar = new T[Math.Min(itemsAtPage, array.Length - j)];for (var i = 0; i < ar.Length; i++)ar[i] = array[j + i];j += ar.Length;result.Add(pageNumber, ar);pageNumber++;}return result;}\r\rThis is a Generic method, T is any type. The function takes a collection of objects and maximum number of objects on the page. Returns a pair <[page number] : [collection of page objects]>\rLike, not exactly shows generalized types, so just in case the screenshot:\r`
by
0 like 0 dislike
DeusModus
\r
Cheese the algorithm just solves the problem. Cheese fact uses ceil — finding the smallest integer that is greater than the received number.
by
0 like 0 dislike
The author in this system, the number of records initially not considered for acceleration.
\r
For example, it displays first 5 pages, poke on the fifth page, if not, then throws you on the last page, which is.
by

0 like 0 dislike