使用PHP自动分页

时间:2014-06-14 15:53:45

标签: php json pagination

目前我正在使用这段代码对已解析的JSON文件进行分页

if ($_GET['page'] == 3){
     $x = 12;
     $y = 16;
 } elseif ($_GET['page'] == 2){
     $x = 6;
     $y = 11;
 } elseif ($_GET['page'] == 1){
     $x = 0;
     $y = 5;
 } elseif ($_GET['page'] == 4){
     $x = 17;
     $y = 21;
 } elseif ($_GET['page'] == 5){
    $x = 22;
    $y = 27;
 } elseif ($_GET['page'] == 6){
    $x = 28;
    $y = 33;
 } elseif ($_GET['page'] == 7){
     $x = 34;
     $y = 39;
 } elseif ($_GET['page'] == 8){
     $x = 40;
     $y = 45;
 } elseif ($_GET['page'] == 9){
     $x = 46;
     $y = 51;
 } elseif ($_GET['page'] == 10){
    $x = 52;
    $y = 57;
 } elseif ($_GET['page'] == 11){
     $x = 58;
     $y = 63;
 }

然后我在$x <= $y

时运行了一个while循环

这可以用更少的代码实现吗?

由于

1 个答案:

答案 0 :(得分:0)

如果你想在每个页面中使用相同数量的行,这样的东西就可以工作...... Syntacs可能是错的,现在很久没有编写PHP代码了

 /* Some variables to help  calculating*/
 $RowsPrPage  = 5;
 $PageNumber = $_GET['page'];

 /* Calculating $x and $y*/
 $x = $RowsPrPage * ($PageNumber-1);
 $y = $x + $RowsPrPage -1;

来自公式的结果:

RowsPrPage = 5 PageNumber = 1 x = 0 y = 4

RowsPrPage = 5 PageNumber = 2 x = 5 y = 9

RowsPrPage = 5 PageNumber = 3 x = 10 y = 14

RowsPrPage = 5 PageNumber = 4 x = 15 y = 19

RowsPrPage = 5 PageNumber = 5 x = 20 y = 24

RowsPrPage = 5 PageNumber = 6 x = 25 y = 29

RowsPrPage = 5 PageNumber = 7 x = 30 y = 34

RowsPrPage = 5 PageNumber = 8 x = 35 y = 39

RowsPrPage = 5 PageNumber = 9 x = 40 y = 44