2015-10-07 70 views
3

我有一個無限的滾動scipt拉數據和顯示它就好了。但是,我發現當您向下滾動數據時,會從頭開始重新開始。現在我有8行用於在數據庫中進行測試,以方便使用。我的控制權獲得下一個數據集似乎沒有工作,否則會轉到下一組結果?無限滾動數據拉

//item per page 
    $limit = 5; 
    $page =(int)(!isset($_GET['p']))?1: $_GET['p']; 

    // sql query 
    $sqlContent="SELECT make, model, year, carid FROM cars"; 

    //Query start point 
    $start =($page * $limit)- $limit; 

    $resContent=$DB_con->query($sqlContent); 
     $rows_returned= $resContent->rowCount();//->fetchColumn(); 

    // query for page navigation 
    if($rows_returned > ($page * $limit)){ 
     $next =++$page; 

    } 


    $sqlContent = $sqlContent ." LIMIT $start, $limit"; 
    $finalContent = $DB_con->query($sqlContent); 
    if($finalContent === false) { 
     trigger_error('Error: ' . $DB_con->error, E_USER_ERROR); 
    } else { 

      $rows_returned= $finalContent->rowCount();//->fetchColumn(); 
    } 
?> 

然後顯示結果:

<?php while($rowContent = $finalContent->fetch()) { 
    $year = $rowContent['year']; 
    $make = $rowContent['make']; 
    $model = $rowContent['model']; 
    ?> 
    <div class="row"> 
     <div class="ride"><?php echo "$year $make $model"; ?></div> 
     </div> 
    <?php } ?> 
    </div> 
     </div> 
    <!--page navigation--> 
    <?php if(isset($next)):?> 
     <div class="nav"> 
      <a href='index.php?p=<?php echo $next?>'>Next</a> 
     </div> 
    <?php endif ?> 
    </div> 
+0

你能否提供JS代碼? – mloureiro

回答

1

這是索姆作爲一名經典的asp程序員,我改編自PHP的日子。

它提供了一個不錯的櫃檯,隨着第一,最後,下一個&上一個環節。

首先是您的sql查詢,其中包含兩條select語句,具體取決於您希望顯示的每頁記錄數和總記錄數。 (如果要顯示的數字實際上大於db中的記錄數)。

<?php 
require'connections/conn.php'; 

$maxRows_rsList = 10; // the number of records you want to show per page 
$pageNum_rsList = 0; 
if (isset($_GET['pageNum_rsList'])) { 
    $pageNum_rsList = $_GET['pageNum_rsList']; 
} 
$startRow_rsList = $pageNum_rsList * $maxRows_rsList; 

$query_rsList = $conn->prepare("SELECT make, model, year, carid FROM cars"); 
$query_limit_rsList = $conn->prepare("SELECT make, model, year, carid FROM cars LIMIT $startRow_rsList, $maxRows_rsList"); 
$query_limit_rsList->execute(); 
$row_rsList = $query_limit_rsList->fetch(PDO::FETCH_ASSOC); 

if (isset($_GET['totalRows_rsList'])) { 
    $totalRows_rsList = $_GET['totalRows_rsList']; 
} else { 
    $all_rsList = $query_rsList->execute(); 
    $totalRows_rsList = $query_rsList->rowCount(); 
} 
$totalPages_rsList = ceil($totalRows_rsList/$maxRows_rsList)-1; 

$queryString_rsList = ""; 
if (!empty($_SERVER['QUERY_STRING'])) { 
    $params = explode("&", $_SERVER['QUERY_STRING']); 
    $newParams = array(); 
    foreach ($params as $param) { 
    if (stristr($param, "pageNum_rsList") == false && 
     stristr($param, "totalRows_rsList") == false) { 
     array_push($newParams, $param); 
    } 
    } 
    if (count($newParams) != 0) { 
    $queryString_rsList = "&" . htmlentities(implode("&", $newParams)); 
    } 
} 
$queryString_rsList = sprintf("&totalRows_rsList=%d%s", $totalRows_rsList, $queryString_rsList); 

$currentPage = $_SERVER["PHP_SELF"]; // so we stay on the same page just changing the recordset data 
?> 

然後我們輸出

<table cellpadding="5" cellspacing="0" border="0"> 
      <?php if($totalRows_rsList > $maxRows_rsList) { ?> 
      <tr> 
      <td colspan="2"><?php echo ($startRow_rsList + 1) ?> to <?php echo min($startRow_rsList + $maxRows_rsList, $totalRows_rsList) ?> of <?php echo $totalRows_rsList ?> cars<br /> 
       <table border="0"> 
      <tr> 
      <?php if ($pageNum_rsList > 0) { // Show if not first page ?> 
      <td width="25" nowrap="nowrap"> 
       <a href="<?php printf("%s?pageNum_rsList=%d%s", $currentPage, 0, $queryString_rsList); ?>">First</a>&nbsp; 
      </td> 
      <?php } // Show if not first page ?> 
      <?php if ($pageNum_rsList > 0) { // Show if not first page ?> 
      <td width="25" nowrap="nowrap"> 
       &nbsp;<a href="<?php printf("%s?pageNum_rsList=%d%s", $currentPage, max(0, $pageNum_rsList - 1), $queryString_rsList); ?>">Prev</a>&nbsp; 
      </td> 
      <?php } // Show if not first page ?> 
      <?php if ($pageNum_rsList < $totalPages_rsList) { // Show if not last page ?> 
      <td width="25" nowrap="nowrap"> 
       &nbsp;<a href="<?php printf("%s?pageNum_rsList=%d%s", $currentPage, min($totalPages_rsList, $pageNum_rsList + 1), $queryString_rsList); ?>">Next</a>&nbsp; 
      </td> 
      <?php } // Show if not last page ?> 
      <?php if ($pageNum_rsList < $totalPages_rsList) { // Show if not last page ?> 
      <td width="25" nowrap="nowrap"> 
       &nbsp;<a href="<?php printf("%s?pageNum_rsList=%d%s", $currentPage, $totalPages_rsList, $queryString_rsList); ?>">Last</a> 
      </td> 
      <?php } // Show if not last page ?> 
      </tr> 
     </table></td> 
    </tr> 
      <?php } else if(($totalRows_rsList == $maxRows_rsList) || ($totalRows_rsList < $maxRows_rsList)) { ?> 
      <tr><td colspan="2">&nbsp;</td></tr> 
      <tr><td colspan="2"><?php echo ($startRow_rsList + 1) ?> to <?php echo min($startRow_rsList + $maxRows_rsList, $totalRows_rsList) ?> of <?php echo $totalRows_rsList ?> cars<br /></td></tr> 
      <?php } ?> 
      <tr><td> 
      <?php do { 
       $year = $row_rsList['year']; 
       $make = $row_rsList['make']; 
       $model = $row_rsList['model']; 
     ?> 
<div class="row"> 
    <div class="ride"><?php echo "$year $make $model"; ?></div> 
    </div> 
<?php } while($row_rsList = $query_limit_rsList->fetch(PDO::FETCH_ASSOC)) ?> 
    </div> 
    </div> 
</td></tr> 
</table> 

編碼快樂!

+0

這實際上對我有幫助!非常感謝! :) –

1
$page =(int)(!isset($_GET['p']))?1: $_GET['p']; 

實際上應該是

$page =(!isset($_GET['p']))?1: (int)$_GET['p']; 

你正在做什麼是鑄造isset的布爾結果作爲整數

+0

什麼都沒有發生。問題依然存在。 –

+1

調試$頁面時發生了什麼?它總是1嗎? –

+0

是的,它總是1. –