我的网页上有一个搜索引擎(PHP)。到目前为止,它将在同一结果页面上显示所有结果。我想将结果数限制为每页20个,并创建一个“下一页”链接......
如果我SELECT TOP 20 * FROM ... WHERE ...
它只显示前20个结果...
如何创建导致结果21到40的<a href="...">NextPage</a>
?
我必须使用AJAX吗?
答案 0 :(得分:2)
如果您正在寻找分页。这http://www.phpeasystep.com/phptu/29.html可能会对您有所帮助。
答案 1 :(得分:2)
<?php<br>
// database connection info<br>
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);<br>
$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);<br>
// find out how many rows are in the table <br>
$sql = "SELECT COUNT(*) FROM numbers";<br>
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);<br>
$r = mysql_fetch_row($result);<br>
$numrows = $r[0];<br>
// number of rows to show per page<br>
$rowsperpage = 10;<br>
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);<br>
// get the current page or set a default<br>
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {<br>
// cast var as int<br>
$currentpage = (int) $_GET['currentpage'];<br>
} else {<br>
// default page num<br>
$currentpage = 1;<br>
} // end if<br>
// if current page is greater than total pages...<br>
if ($currentpage > $totalpages) {<br>
// set current page to last page<br>
$currentpage = $totalpages;<br>
} // end if<br>
// if current page is less than first page...<br>
if ($currentpage < 1) {<br>
// set current page to first page<br>
$currentpage = 1;<br>
} // end if<br>
// the offset of the list, based on current page <br>
$offset = ($currentpage - 1) * $rowsperpage;<br>
// get the info from the db <br>
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";<br>
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);<br>
// while there are rows to be fetched...<br>
while ($list = mysql_fetch_assoc($result)) {<br>
// echo data<br>
echo $list['id'] . " : " . $list['number'] . "<br />";<br>
} // end while
/****** build the pagination links ******/<br>
// range of num links to show<br>
$range = 3;<br>
// if not on page 1, don't show back links<br>
if ($currentpage > 1) {<br>
// show << link to go back to page 1<br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";<br>
// get previous page num<br>
$prevpage = $currentpage - 1;<br>
// show < link to go back to 1 page<br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";<br>
} // end if <br>
// loop to show links to range of pages around current page<br>
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {<br>
// if it's a valid page number...<br>
if (($x > 0) && ($x <= $totalpages)) {<br>
// if we're on current page...<br>
if ($x == $currentpage) {<br>
// 'highlight' it but don't make a link<br>
echo " [<b>$x</b>] ";<br>
// if not current page...<br>
} else {<br>
// make it a link<br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";<br>
} // end else<br>
} // end if <br>
} // end for<br>
// if not on last page, show forward and last page links <br>
if ($currentpage != $totalpages) {<br>
// get next page<br>
$nextpage = $currentpage + 1;<br>
// echo forward link for next page <br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";<br>
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";<br>
} // end if<br>
/****** end build pagination links ******/<br>
?>
答案 2 :(得分:1)
您使用的前端技术是什么? Java,.NET,PHP等?
您尝试实现的分页包含在大多数最新的Web框架中。您不需要自己手动实现分页逻辑。
即使您出于某种原因需要手动执行,也可以 -
SELECT * FROM TABLE1 WHERE .... LIMIT FROM, TO
。
每页增加FROM
和TO
20。