简体   繁体   中英

Infinite scroll data pull

I have an infinite scroll scipt that is pulling data and displaying it just fine. However, I am finding that when you scroll down the data pull starts again at the beginning. Right now I have 8 rows for testing in the database to make it easy. My control to get the next data set does not seem to be working otherwise it would go to the next set of results?

//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();  
    }  
?>  

then display the results:

<?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>

This is something that I adapted for PHP from my days as a classic-asp programmer.

It provides a nice counter along with First, Last, Next & Previous links.

First is your sql query with two select statements depending on the number of records you want to show per page AND the total number of records. (in case the number you want to show is actually larger than the number of records in the 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
?>

Then our output

<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>

Happy Coding !

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

should actually be

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

what you are doing is casting the boolean result of isset as an integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM