2013-05-13 67 views
0

我的網頁上有一個搜索引擎(PHP)。到目前爲止,它會在同一結果頁面上顯示所有結果。我想結果的數量限制在20個每頁,並創建一個「下一頁」鏈接...如何創建「下一頁」鏈接到mySQL搜索的「未顯示」結果?

如果我做SELECT TOP 20 * FROM ... WHERE ... 這將只顯示前20個結果......

怎麼辦我創建了<a href="...">NextPage</a>這將導致結果21到40? 我必須使用AJAX嗎?

+0

這就是所謂的分頁。我不確定你是怎麼做到的,但希望這個詞能給你別的東西來幫助你找出答案。 – 2013-05-13 06:15:50

+0

@Michael N謝謝。我會檢查出谷歌 – CHEBURASHKA 2013-05-13 06:17:01

回答

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

您正在使用的前端技術是什麼? Java,.NET,PHP等?

您嘗試實現的分頁內置於大多數最新的Web框架中。你不需要自己手動實現分頁邏輯。

即使你需要某種原因做手工,你可以做 -

SELECT * FROM TABLE1 WHERE .... LIMIT FROM, TO。 每頁增加FROMTO 20。

+0

謝謝。我正在使用PHP。我會試試這個 – CHEBURASHKA 2013-05-13 06:20:53

相關問題