PHP Formula For a Series of Numbers (Mathy Problem)

…衆ロ難τιáo~ 提交于 2019-12-06 15:37:55

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

Also check Zend_Paginator, is build exactly for what you need.

RobertPitt

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:

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:

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++).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!