为什么网站去第一页浏览器的后退按钮,而不是去上一个页面中的分页

为什么网站去第一页浏览器的后退按钮,而不是去上一个页面中的分页

问题描述:

更新:因为我已经一个星期,因为有许多线程一直在寻找解决方案,如thisthis 等等,但找不到有用的东西。为什么网站去第一页浏览器的后退按钮,而不是去上一个页面中的分页

我有一个汽车交易网站,并有大量的数据库记录。我已经尝试过实现分页,现在有两种方法,我已经实现了一个接一个观察。

  • 创建分页与循环和链接到每个按钮,如1 2 3.这会导致整个页面刷新每个链接/按钮单击。
  • 使用jquery和AJAX创建分页,以避免整个页面刷新,这也很好。

我正在使用第二种方法,问题是,如果我例如移动到第3页并选择了一个将我带到汽车经销商档案中的汽车经销商。现在,当我按下浏览器的后退按钮,它带我去的第一页,而应该我重定向回第3页。

我来自哪里的问题是产生了我的数据库查询的,因为拿到知道记录,在返回到之前的(家/索引)页面时重新运行。 我的上一页是home/index页面,所有记录都显示在此页面上。

问题是,

  • 如何避免数据库查询得到重新运行,或者如何检查,如果页面已经加载和查询存在。

PHP代码:

<?php 
include 'connect.php'; 
if(isset($_GET['pId'])){ 
    $page_num = $_GET['pId']; 
}else{ 
    $page_num = 1; 
} 
$filter_qry = "SELECT * from showroom"; 
$sh_result = $conn->query($filter_qry); 
$count = mysqli_num_rows($sh_result); 
$p_num = 4; 
$rem = $count/$p_num; 
$rem = ceil($rem); 
if($page_num == "" || $page_num == 1){ 
    $show_page = 0; 
}else{ 
    $show_page =($page_num*$p_num)-$p_num; 
} 
$filter_qry = $filter_qry." limit $show_page,$p_num"; //concatinating the limit with query 
$sh_result1 = $conn->query($filter_qry); 
if($sh_result1->num_rows >= 1){ 
    while($row1 = mysqli_fetch_assoc($sh_result1)){ 
     $image_path = $image_name = $target_path = ""; 
     $_show_name = $row1['sh_name']; 
     $_show_place = $row1['sh_street']; 
     $_show_city = $row1['sh_city']; 
     $_show_country = $row1['sh_country']; 
     $_sh_phone = $row1['sh_phone']; 
     $_sh_id = $row1['sh_id']; ?> 
     <div id="dem" class="w3-col m3 w3-section" > 
      <div class="w3-container"> 
       <a style="text-decoration: none" href="showroom_display.php?sh_id=<?php echo $_sh_id; ?>"> 
        <div class="w3-display-container w3-green w3-center" style="max-width: 100%; height: 20%"> 
         <span class="w3-medium w3-display-middle" style="width: 80%"><i class="fa fa-home" style="font-size: 30px"></i><br><b><?php echo $_show_name; ?></b></span> 
        </div> 
        <div class="w3-white w3-padding"> 
         <h5 class="w3-text-gray"><small><i class="fa fa-street-view"></i><?php echo " ".$_show_place;?> </small></h5> 
         <h5 class="w3-border-bottom w3-text-gray"><i class="fa fa-map-marker"></i><small><?php echo " ".$_show_city;?> </small></h5> 
         <span class="w3-medium w3-text-green"><small>call : </small><b><?php echo $_sh_phone; ?></b></span> 
        </div> 
       </a> 
      </div> 
     </div> 
     <?php 
    } 
}else{ 
    ?> 
    <script> 
     alert("No matching Showroom."); 
     window.location.href='index.php'; 
    </script> 
    <?php 
} ?> 

HTML代码

<div class="w3-container w3-light-grey"> 
<div id="tar" class="w3-content w3-row"> 
    <?php include "pagination.php"; ?> 
</div> 
<div class="w3-center w3-row-padding w3-margin-top w3-padding-16"> 
    <?php for($l = 1; $l <= $rem; $l++){ ?> 
     <a class="w3-green w3-button w3-hover-green w3-round-xlarge" onclick="get_page(<?php echo $l;?>);"><?php echo $l; ?></a> 
    <?php } ?> 
</div> 

javascript代码

<script> 
function get_page(PageId){ 
    $.get("pagination.php", { pId: PageId},function(postresult){ 
    $("#tar").html(postresult); 
    }); 
} 

我希望我的问题是清楚的,我不会混淆任何人。

+0

向我们显示代码或我们无法帮助 – delboy1978uk

+1

至少您对问题原因的猜测是错误的 - 这不是因为数据库。浏览器后退按钮应该带你到上一页,它存储在历史*中。因此,对于以编程方式创建分页的情况,直到您还处理html历史记录时才能工作 –

+0

您需要将分页事件推送到JavaScript历史记录对象(例如/page.html?id=2),每次“页面”通过你的结果。然后,您可以在页面加载时实现一个调用(服务器或客户端),查找查询字符串参数,如果存在,则显示该页面的结果,否则显示第一页。将它们创建为物理页面(选项1)可解决此问题,但当然会导致用户不得不重新加载页面,但这些是您的选择。 – delinear

你真正想要的是服务器端分页,其中cleary和肯定,第一种方法将不适用于分页,因为它涉及形式submition。

对于第二种方法,您需要更改逻辑。在分页的每个按钮的点击,你应该页码和要显示在同一时间

例如,对于第二页行的号码发送Ajax请求,就发送页面编号= 2,pageSize的= 10(假设页面大小)如此在服务器端(php)准备SQL语句跳过前10条记录,然后只取下10条记录。现在将这些数据作为JSON响应发送回来,并将此数据显示为ajax成功回调或您正在使用的任何方法。


This解释了如何跳过并记录下一个SQL。

希望这有助于。