doushao8399 2017-01-06 10:04
浏览 10
已采纳

http_build_query将20添加到分页

I've just started using http_build_query. I've implemented it into a pagination script of mine however it's not working right.

What I am expecting is for my urls to be http://example.com/categories.php?cat=category&page=2 but for some reason it is adding 20 to my page numbers making the urls like so http://example.com/categories.php?cat=category&page=22 <<< This is actually for page 2.

Here is my original script:

  $img_start=0;
  $img_limit=8;

  if(isset($_GET['page'])) {
   $page=$_GET['page'];
   $img_start=($page-1)*$img_limit;
  }
  else { $page = 1; }

  if($_COOKIE['age_verification'] == "adult") {
   $img_total = mysqli_num_rows(mysqli_query($conn, "select * from gallery_img WHERE $cat = 1"));
  }
  else if($_COOKIE['age_verification'] == "child") {
   $img_total = mysqli_num_rows(mysqli_query($conn, "select * from gallery_img WHERE $cat = 1 AND WHERE nude != 1"));
  }
  $img_total_count = ceil($img_total/$img_limit);

  if($img_limit != $img_total) {
  echo '<nav aria-label="Page navigation">
         <ul class="pagination">'  . PHP_EOL;
  if($page>1) {
  echo '<li><a href="?page='.($page-1).'" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>'  . PHP_EOL;
  }
  for($i=1;$i<=$img_total_count;$i++) {
   if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>"  . PHP_EOL; }
   else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"  . PHP_EOL; }
  }
  if($page!=$img_total_count) {
   if(!isset($page)) { echo '<li><a href="?page='.($page+2).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
   else { echo '<li><a href="?page='.($page+1).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
  }

echo '</ul>
      </nav>' . PHP_EOL;
  }

And then these are the lines I modified which are adding 20 to my page numbers:

$pageParameters = http_build_query(array_merge($_GET, array("page"=>2)));

  if($img_limit != $img_total) {
  echo '<nav aria-label="Page navigation">
         <ul class="pagination">'  . PHP_EOL;
  if($page>1) {
  echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page-1).'" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>'  . PHP_EOL;
  }
  for($i=1;$i<=$img_total_count;$i++) {
   if($i==$page) { echo "<li class='active'><a href='".htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").$i."'>".$i."</a></li>"  . PHP_EOL; }
   else { echo "<li><a href='".htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").$i."'>".$i."</a></li>"  . PHP_EOL; }
  }
  if($page!=$img_total_count) {
   if(!isset($page)) { echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page+2).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
   else { echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page+1).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
  }

echo '</ul>
      </nav>' . PHP_EOL;
  }

Why would this be adding 20 to my page numbers?

展开全部

  • 写回答

2条回答 默认 最新

  • duanlan4801 2017-01-06 10:39
    关注

    Your code is not adding 20 to the existing page number, you're actually doing a concatenation here, which is wrong. Look at the following code snippet,

    ... htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page-1) ...
                                                              ^ see here
    

    If your existing URL is http://example.com/categories.php?cat=category&page=2, 2 would simply get appended after the above operation, making it http://example.com/categories.php?cat=category&page=22. And this holds true for all the cases where you have used the above operation. So the solution would be to change your pagination links section like this:

    parse_str($_SERVER["QUERY_STRING"], $url_array);
    unset($url_array['page']);
    $pageParameters = http_build_query($url_array);
    
    if($img_limit != $img_total) {
        echo '<nav aria-label="Page navigation"><ul class="pagination">';
        if($page>1) {
            ?>
            <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($page-1); echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>
            <?php
        }
        for($i=1;$i<=$img_total_count;$i++) {
            if($i==$page) { 
                ?>
                <li class='active'><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$i; echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>"><?php echo $i; ?></a></li>
                <?php
            }
            else { 
                ?>
                <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$i; echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>"><?php echo $i; ?></a></li>
                <?php
            }
        }
        if($page!=$img_total_count) {
            if(!isset($page)) { 
                ?>
                <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($page+2); echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>" aria-label="Next"><span aria-hidden="true">Next</span></a></li>
                <?php
            }else { 
                ?>
                <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($page+1); echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>" aria-label="Next"><span aria-hidden="true">Next</span></a></li>
                <?php
            }
        }
        echo '</ul></nav>';
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 使用X11可以找到托盘句柄,监控到窗口点击事件但是如何在监听的同时获取托盘中应用的上下文菜单句柄
  • ¥15 IEd中开关量采样信号通道设计
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部