本文實(shí)例講述了PHP+ajax分頁(yè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
HTML代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<html> <head> <meta http-equiv= "Content-Language" content= "en" /> <meta name= "GENERATOR" content= "PHPEclipse 1.0" /> <meta http-equiv= "Content-Type" content= "text/html; charset=GB2312" /> <title>title</title> <mce:script type= 'text/javascript' ><!-- var http_request= false ; function send_request(url){ //初始化,指定處理函數(shù),發(fā)送請(qǐng)求的函數(shù) http_request= false ; //開(kāi)始初始化XMLHttpRequest對(duì)象 if (window.XMLHttpRequest){ //Mozilla瀏覽器 http_request= new XMLHttpRequest(); if (http_request.overrideMimeType){ //設(shè)置MIME類(lèi)別 http_request.overrideMimeType( "text/xml" ); } } else if (window.ActiveXObject){ //IE瀏覽器 try { http_request= new ActiveXObject( "Msxml2.XMLHttp" ); } catch (e){ try { http_request= new ActiveXobject( "Microsoft.XMLHttp" ); } catch (e){} } } if (!http_request){ //異常,創(chuàng)建對(duì)象實(shí)例失敗 window.alert( "創(chuàng)建XMLHttp對(duì)象失敗!" ); return false ; } http_request.open( "GET" ,url, true ); http_request.onreadystatechange=processrequest; //確定發(fā)送請(qǐng)求方式,URL,及是否同步執(zhí)行下段代碼 http_request.send( null ); } //處理返回信息的函數(shù) function processrequest(){ if (http_request.readyState==4){ //判斷對(duì)象狀態(tài) if (http_request.status==200){ //信息已成功返回,開(kāi)始處理信息 document.getElementById( 'result' ).innerHTML=http_request.responseText; } else { //頁(yè)面不正常 alert( "您所請(qǐng)求的頁(yè)面不正常!" ); } } } function dopage(url) { document.getElementById( 'result' ).innerHTML= "正在讀取數(shù)據(jù)..." ; send_request(url); } // --></mce:script> </head> <body bgcolor= "#FFFFFF" text= "#000000" link= "#FF9966" vlink= "#FF9966" alink= "#FFCC99" > <div id= 'result' > </div> </body> </html> |
php代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
header( "Content-type: text/html;charset=GBK" ); // $link =mysql_connect( 'localhost' , 'root' , 'root' ); mysql_select_db( 'test' ); mysql_query( "SET NAMES 'GBK'" ); $num =5; //每頁(yè)顯示5條 $sql = "select * from article " ; //總共有多少記錄 $resultt =mysql_query( $sql ); //獲取數(shù)據(jù)庫(kù)總記錄數(shù) $total =mysql_num_rows( $result ); echo $total ; //總共有多少頁(yè) $pagecount = $total / $num ; //獲取當(dāng)前頁(yè) $currentPage =isset( $_GET [ 'page' ])? intval ( $_GET [ 'page' ]):1; //查詢(xún)條數(shù) $offset =( $currentPage -1)* $num ; //下一頁(yè) $nexpage =( $currentPage == $pagecount ?0: $currentPage +1); //上一頁(yè) $prepg = $currentPage -1; $url = 'ajax.php' ; //根據(jù)條數(shù)查詢(xún)信息 $strSQL = "select * from article limit $offset,$num" ; $pagecontrol = "當(dāng)前第" . $currentPage . "頁(yè)" ; $pagecontrol .= "共" . $pagecount . "頁(yè)" ; $pagecontrol .= "<a href=" javascript:dopage( " mce_href=" javascript:dopage( "'$url?page=1')>首 頁(yè)</a> " ; if ( $prepg ) $pagecontrol .= " <a href=" javascript:dopage( " mce_href=" javascript:dopage( "'$url?page=$prepg');>上一頁(yè)</a> " ; else $pagecontrol .= " 上一頁(yè) " ; if ( $nexpage ) $pagecontrol .= "<a href=" javascript:dopage( " mce_href=" javascript:dopage( "'$url?page=$nexpage');>下一頁(yè)</a>" ; else $pagecontrol .= "下一頁(yè)" ; $pagecontrol .= "<a href=" javascript:dopage( " mce_href=" javascript:dopage( "'$url?page=$pagecount')>尾 頁(yè)</a>" ; $result1 =mysql_query( $strSQL ); echo "<table border=1 width=100%>" ; echo "<tr>" ; echo "<td>編 號(hào)</td><td>標(biāo) 題</td><td>新聞?lì)愋?lt;/td>" ; echo "</tr>" ; while ( $row =mysql_fetch_array( $result1 )) { echo "<tr>" ; echo "<td>$row[id]</td><td>$row[title]</td><td>$row[classname]</td>" ; echo "</tr>" ; } echo "</table>" ; echo "<br/>" ; echo $pagecontrol ; |
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。