0

我的网页上有一个搜索引擎(PHP)。到目前为止,它将在同一结果页面上显示所有结果。我想将结果数限制为每页 20 个,并创建一个“下一页”链接...

如果我这样做SELECT TOP 20 * FROM ... WHERE ... ,它只会显示前 20 个结果...

如何创建<a href="...">NextPage</a>会导致结果 21 到 40 的结果?我必须使用 AJAX 吗?

4

3 回答 3

2

如果您正在寻找分页。这个http://www.phpeasystep.com/phptu/29.html可以帮助你。

于 2013-05-13T06:39:38.217 回答
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>
?>
于 2013-05-13T07:11:22.457 回答
1

您使用的前端技术是什么?Java、.NET、PHP 等?

您尝试实现的分页是在大多数最新的网络框架中内置的。您不需要自己手动实现分页逻辑。

即使您出于某种原因需要手动执行此操作,您也可以执行 -

SELECT * FROM TABLE1 WHERE .... LIMIT FROM, TO. FROM每页递增TO20。

于 2013-05-13T06:19:16.080 回答