问题
My apologies if this is the wrong site for this problem, as it is more math-related than programming.
I am trying to write a series of 7 page links, in a Google-esque fashion. Essentially, it will be 7 numbers, s to (s + 6), where s is my starting value. I am having trouble calculating my starting value, given a limited amount of information.
In advance, I know the maximum value in the series, this is variable, but it is always greater than 7. In my formula-writing attempts, I have been calling this value g, so g > 7.
I also know the page number the user has selected. I have been calling this value p
So, for example, if g was 8, I would need to generate these series of numbers, where the bolded number is equal to p:
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
2 3 4 5 6 7 8
2 3 4 5 6 7 8
2 3 4 5 6 7 8
2 3 4 5 6 7 8
As long as I can determine the starting value using the information available, everything else falls into place. Can anyone advise on how I would calculate my starting value using the information available? If it is relevant, I will be writing this formula in PHP.
Thanks in advance for any input.
回答1:
This is just simulation code for testing.
<?
$g=16;
for($p=1;$p<17;$p++){
$start = $g-$p > 3 ? ($p-4<1?1:($p-4)) : $g-6;
echo "$p :: ";
for($i=$start;$i<$start+7;$i++){
echo $i . " ";
}
echo "<br>";
}
?>
SO your Start Page is decided by (the thing you actually need):
$start = $g-$p > 3 ? ($p-4<1?1:($p-4)) : $g-6;
Output (Simulation for g=16, and p from 1 to 16)::
p :: page numbers
1 :: 1 2 3 4 5 6 7
2 :: 1 2 3 4 5 6 7
3 :: 1 2 3 4 5 6 7
4 :: 1 2 3 4 5 6 7
5 :: 1 2 3 4 5 6 7
6 :: 2 3 4 5 6 7 8
7 :: 3 4 5 6 7 8 9
8 :: 4 5 6 7 8 9 10
9 :: 5 6 7 8 9 10 11
10 :: 6 7 8 9 10 11 12
11 :: 7 8 9 10 11 12 13
12 :: 8 9 10 11 12 13 14
13 :: 10 11 12 13 14 15 16
14 :: 10 11 12 13 14 15 16
15 :: 10 11 12 13 14 15 16
16 :: 10 11 12 13 14 15 16
And simulation for g=8, p from 1 to 8
1 :: 1 2 3 4 5 6 7
2 :: 1 2 3 4 5 6 7
3 :: 1 2 3 4 5 6 7
4 :: 1 2 3 4 5 6 7
5 :: 2 3 4 5 6 7 8
6 :: 2 3 4 5 6 7 8
7 :: 2 3 4 5 6 7 8
8 :: 2 3 4 5 6 7 8
回答2:
Also check Zend_Paginator, is build exactly for what you need.
回答3:
This is refereed to as pagination and can be be done by doing the following:
- Firstly you would need a result set, usually from the database or where ever.
- After applying any filters to the result set you should have another result set
- You will need the count of the filtered results
- You will need a variable that is set to the "per page limit"
- a variable containing the current page
- a varaiable that states how many links should be either side of the active link
Here is some code that i wrote for pagination in one of my project's, it's in the form of a class though im not sure on your level of skill but will provide you help with the Math:
- https://github.com/AdminSpot/ASDDL/blob/master/system/libraries/pagination.php
Although my class may seem complex as it's used for pagination such as
< << 1 2 3 ... 10 11 12 ... 19 20 21 >> >
using adjectives and what not.
also ckeck out the following links:
- php calculation solution for pagination
回答4:
Work backward. You know the end number, so instead of doing s++
, do g--
in the loop to output the numbers. In the process, you can check to see if g == p
and if so, add your styling to it.
Depending on your specific code, you might have to do two loops, one to get the array of numbers, and one to output them in the correct order, but that's pretty trivial. If you really need/want to stick to one loop, then you could find s
with some simple math: s = g - 6
, then you can work up from there in a loop that increments s (s++
).
来源:https://stackoverflow.com/questions/6538637/php-formula-for-a-series-of-numbers-mathy-problem