dongshan7060 2014-04-11 20:26
浏览 17

PHP从分页中删除第0页

I was able to get my pagination script working, and everything works fine except when you go to PAGE 2, you can see a PAGE 0, which if you click on it give you nothing.

I need to fix my current code so that the application does not show a PAGE 0.

Here is the necessary code:

 <?php
   $total_records = countRecords(); // self explanatory function called here
   $total_pages = ceil($total_records / $rec_limit);
   $adjacents = 2;
   $previousPage = $page - 1;
   $nextPage = $page + 1;
   $querystring = "";
   $start = ($page < $adjacents ? 1 : $page - $adjacents);
   $beginning = 1;
   $end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);

   foreach($_GET as $key => $value)
   {
     if($key != "page") $querystring .= "$key=$value&amp;";
   }
   echo 
     '<div class="row-fluid">
      <div class="span2">'.countRecords()." total records" .'</div>
      <div class="container pagination-small">
      <ul style="margin: 3px;" class="pager">';
   echo @"<li><a href=\"$targetpage?page=$beginning&$querystring\">First</a></li>";
   if ($left_rec < $rec_limit) 
   {
     $last = $page - 1;
     echo @"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
     Previous</a></li>";
     for($i= $start; $i <= $end; $i++)
     {
       echo @"<li  " . ((($page)==$i)? "class=\"active\"" : "") . ">
       <a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
   }
   else if($page == 0)
   {
     for($i= $start; $i <= $end; $i++)
     {
       echo @"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">
       <a href=\$targetpage?page=$i&$querystring\">$i</a></li>";
     }
       echo @"<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
   }
   else if ($page > 0)
   {
     $last = $page - 2;
     echo @"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
     Previous</a></li>";
     for($i= $start; $i <= $end; $i++)
     {
       echo @"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">
       <a href=\"targetpage?page=$i&$querystring\">$i</a></li>";
     }
      echo @"<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a><li>";
   }
      echo @"<li><a href=\"$targetpage?page=$total_pages&$querystring\">Last</a></li>";
   echo '<ul></div></div>';

I tried to keep the code as short as possible. If there are any errors, just note it's only a typo as I typed it here. The code works, with exception to my problem.

So with that all said, can anyone tell me how to remove PAGE 0 from the pagination? I have done some research, but I have been unsuccessful applying it to my code. So I'm hoping someone can take a look at my code and tell me how I can alter it to make this work.

I appreciate the help.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongsan6889 2014-04-11 20:31
    关注

    Logic flaw:

    $start = ($page < $adjacents ? 1 : $page - $adjacents);
    

    If you're on Page 1, you'll get

    $start = (1 < 2 ? 1 : 1 - 2);
    $start = -1; // page negative one? huh?
    

    Then this loop is pointless:

    foreach($_GET as $key => $value) { ... }
    

    Why not just

    unset($_GET['page']);
    $q = http_build_query($_GET);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法