我有一个无限滚动的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 :(得分:2)
从我作为经典asp程序员的日子开始,这是我适应PHP的东西。
它提供了一个很好的计数器以及First,Last,Next&amp;以前的链接。
首先是带有两个select语句的sql查询,具体取决于每页要显示的记录数和记录总数。 (如果您要显示的数字实际上大于数据库中的记录数。)
<?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>
</td>
<?php } // Show if not first page ?>
<?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, max(0, $pageNum_rsList - 1), $queryString_rsList); ?>">Prev</a>
</td>
<?php } // Show if not first page ?>
<?php if ($pageNum_rsList < $totalPages_rsList) { // Show if not last page ?>
<td width="25" nowrap="nowrap">
<a href="<?php printf("%s?pageNum_rsList=%d%s", $currentPage, min($totalPages_rsList, $pageNum_rsList + 1), $queryString_rsList); ?>">Next</a>
</td>
<?php } // Show if not last page ?>
<?php if ($pageNum_rsList < $totalPages_rsList) { // Show if not last page ?>
<td width="25" nowrap="nowrap">
<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"> </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>
快乐的编码!
答案 1 :(得分:1)
$page =(int)(!isset($_GET['p']))?1: $_GET['p'];
实际应该是
$page =(!isset($_GET['p']))?1: (int)$_GET['p'];
您正在做的是将isset的布尔结果转换为整数