2017-10-19 24 views
0

更新:因爲我已經一個星期,因爲有許多線程一直在尋找解決方案,如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

回答

0

你真正想要的是服務器端分頁,其中cleary和肯定,第一種方法將不適用於分頁,因爲它涉及形式submition。

對於第二種方法,您需要更改邏輯。在分頁的每個按鈕的點擊,你應該頁碼和要顯示在同一時間

例如,對於第二頁行的號碼發送Ajax請求,就發送頁面編號= 2,pageSize的= 10(假設頁面大小)如此在服務器端(php)準備SQL語句跳過前10條記錄,然後只取下10條記錄。現在將這些數據作爲JSON響應發送回來,並將此數據顯示爲ajax成功回調或您正在使用的任何方法。


This解釋瞭如何跳過並記錄下一個SQL。

希望這有助於。

相關問題