In the PHP Web site, there are many needs to achieve the paging effect, mainly for reading novels.
Features are implemented as follows:
When you use SQL queries for paging, you need to call several functions, specifically the script:
1.pager.class.php
Class Pager {
Public $sql; SQL query Statements
Public $datanum; Query the total number of records for all data
Public $page _size; Shows the number of records per page
protected $_errstr;
protected $_conn;
protected $_query_id;
Public Function query ($query)///This function has a problem, temporarily can not
{
$ret = false;
if (!empty ($query)) {
if ($this->_conn = = = False | |!is_resource ($this->_conn)) {
Warninglog (__method__. ': Query SQL with no connection ', true);
return false;
}
$this->_query_id = @mysql_query ($query, $this->_conn);
if ($this->_query_id = = = False) {
$this->_errstr = @mysql_error ();
$ret = false;
} else {
$this->_errstr = ' SUCCESS ';
$ret = $this->_query_id;
}
}
$msg = ($ret = = = False)? ' False ': Strval ($ret);
Debuglog (__method__. ": [$msg] returned for SQL query [$query]");
return $ret;
}
function __construct ($sql, $page _size) {
$result = mysql_query ($sql);
$datanum = mysql_num_rows ($result);
$this->sql= $sql;
$this->datanum= $datanum;
$this->page_size= $page _size;
}
Current page
Public Function page_id () {
if ($_server[' query_string ' = = "") {
return 1;
}elseif (Substr_count ($_server[' query_string '), "page_id=") = = 0) {
return 1;
}else{
Return Intval (substr ($_server[' query_string '],8));
}
}
Remaining URL value
Public Function URL () {
if ($_server[' query_string ' = = "") {
Return "";
}elseif (Substr_count ($_server[' query_string '), "page_id=") = = 0) {
Return "&". $_server[' query_string ');
}else{
Return Str_replace ("page_id=". $this->page_id (), "", $_server[' query_string ']);
}
}
Total pages
Public Function Page_num () {
if ($this->datanum = = 0) {
return 1;
}else{
Return Ceil ($this->datanum/$this->page_size);
}
}
The offset of the database query
Public Function Start () {
Return ($this->page_id ()-1) * $this->page_size;
}
Data output
Public Function sqlquery () {
return $this->sql. "Limit". $this->start (). ",". $this->page_size;
}
Gets the current file name
Private Function php_self () {
Return $_server[' php_self '];
}
Previous page
Private Function Pre_page () {
if ($this->page_id () = = 1) {//pages equals 1
Return "Php_self ()."? Page_id=1 ". $this->url ()." > Previous page ";
}elseif ($this->page_id ()! = 1) {//pages not equal to 1
Return "Php_self ()."? Page_id= ". ($this->page_id ()-1). $this->url (). " > Previous page ";
}
}
Show pagination
Private Function Display_page () {
$display _page = "";
if ($this->page_num () <= 10) {//less than 10 pages
for ($i =1; $i <= $this->page_num (); $i + +)//Cycle Show page
$display _page. = "Php_self ()."? Page_id= ". $i. $this->url ()." > ". $i." ";
return $display _page;
}elseif ($this->page_num () > 10) {//greater than 10 pages
if ($this->page_id () <= 6) {
for ($i =1; $i <=10; $i + +)//Cycle Show page
$display _page. = "Php_self ()."? Page_id= ". $i. $this->url ()." > ". $i." ";
return $display _page;
}elseif (($this->page_id () > 6) && ($this->page_num ()-$this->page_id () >= 4)) {
for ($i = $this->page_id ()-5; $i <= $this->page_id () +4; $i + +)//Cycle Display page
$display _page. = "Php_self ()."? Page_id= ". $i. $this->url ()." > ". $i." ";
return $display _page;
}elseif (($this->page_id () > 6) && ($this->page_num ()-$this->page_id () < 4)) {
for ($i = $this->page_num ()-9; $i <= $this->page_num (); $i + +)//Cycle Display page
$display _page. = "Php_self ()."? Page_id= ". $i. $this->url ()." > ". $i." ";
return $display _page;
}
}
}
Next page
Private Function Next_page () {
if ($this->page_id () < $this->page_num ()) {//pages less than the total number of pages
Return "Php_self ()."? Page_id= ". ($this->page_id () +1). $this->url (). " > next Page ";
}elseif ($this->page_id () = = $this->page_num ()) {//pages equal to total pages
Return "Php_self ()."? Page_id= ". $this->page_num (). $this->url ()." > next Page ";
}
}
Set Paging information
Public Function Set_page_info () {
$page _info = "Total". $this->datanum. " Article ";
$page _info. = "Php_self ()."? Page_id=1 ". $this->url ()." > Home ";
$page _info. = $this->pre_page ();
$page _info. = $this->display_page ();
$page _info. = $this->next_page ();
$page _info. = "Php_self ()."? Page_id= ". $this->page_num (). $this->url ()." > Last ";
$page _info. = "First". $this->page_id (). " /". $this->page_num ()." Page ";
return $page _info;
}
}
?>
2. Script 2:
Usage of the class
Reading a paging class
Include ("pager.class.php");
Database connection Initialization
$db = new MySQL ();
$impeach _host = ' 10.81.43.139 ';
$impeach _usr = ' vmtest15 ';
$impeach _passwd = ' vmtest15 ';
$impeach _name = ' ufeature ';
$impeach _con = mysql_connect ($impeach _host, $impeach _usr, $impeach _passwd) or
Die ("Can ' t connect". Mysql_error ());
mysql_select_db ($impeach _name, $impeach _con);
This is a SQL query statement and gets the query results
$sql = "Select Word from ufeature.spam_accuse_word_list where flag= ' 0";
Paging initialization
$page = new Pager ($sql, 20);
20 is the number displayed per page
$res _1 = mysql_query ($sql) or
Die ("Can ' t get result". Mysql_error ());
$result =mysql_query ($page->sqlquery ());
while ($info = Mysql_fetch_array ($result, Mysql_assoc)) {
while ($info = mysql_fetch_array ($res _1, Mysql_assoc)) {
echo $info ["word"].
";
}
Page Index Bar
echo $page->set_page_info ();
?>
Second, the method of using Ajax
1. First understand the limit usage in SQL statements
SELECT * FROM table ... limit start position, number of operation bars (where starting position is starting from 0)
Example:
Take the first 20 records: SELECT * from Table ... limit 0, 20
Starting from 11th, fetch 20 records: SELECT * FROM Table ... limit 10, 20
Limit n is equivalent to limit 0,n.
such as SELECT * from table LIMIT 5; Returns the first 5 rows, like the select * from table LIMIT 0,5
2. Paging principle
The so-called paging display, that is, the result set in the database, a section of the display
How to segment, current in the first paragraph (there are several pages per page, the current second page)
Top 10 Records: SELECT * FROM table limit 0,10
11th to 20th record: SELECT * FROM table limit 10,10
21st to 30th Record: SELECT * FROM table limit 20,10
page out formula:
(Current page-1) X number of pages per page, number of articles per page
Select * FROM table limit ($Page-1) * $PageSize, $PageSize
3, $_server["Request_uri"] function
One of the predefined server variables, all at the beginning of the $_server, is called a predetermined server variable.
The function of Request_uri is to obtain the current URI, as well as the full address path behind the domain name.
Example:
The current page is: http://www.test.com/home.php?id=23&cid=22
echo $_server["Request_uri"]
The result is:/home.php?id=23&cid=22
4. Parse_url () Parse URL function
Parse_url () is a function that parses a URL into an array with fixed key values.
Example
$ua =parse_url ("Http://username:password@hostname/path?arg=value#anchor");
Print_r ($ua);
Results:
Array
(
[Scheme] = HTTP; protocol
[Host] = hostname; Host domain name
[User] = Username;
[Pass] = password; password
[path] =/path;
[query] = arg=value; take parameters
[Fragment] = anchor;
)
5. code example
The page of this message is divided into 3 parts, one is the database design, one is the connection page, and the other is the display page.
(1) Design Database
Design database named BBS, there is a data table for the message, which contains title,lastdate,user,content and other fields, respectively, the message title, message before, message people, the content of the message
(2) connection page
$conn = @ mysql_connect ("localhost", "root", "123456") or Die ("Database link error");
mysql_select_db ("BBS", $conn);
mysql_query ("Set names ' GBK '"); Use GBK Chinese code;
Converts a space, newline, to HTML resolvable
function Htmtocode ($content) {
$content = Str_replace ("\ n", "
", Str_replace (" "," ", $content)); Two x Str_replace nesting
return $content;
}
$content =str_replace ("'", "'", $content);
Htmlspecialchars ();
?>
(3) display page
Include ("conn.php");
$pagesize = 2; Set display 2 records per page
$url =$_server["Request_uri"];
$url =parse_url ($url);
$url = $url [path];
$NUMQ =mysql_query ("SELECT * from ' message ');
$num = mysql_num_rows ($NUMQ);
if ($_get[page]) {
$pageval =$_get[page];
$page = ($pageval-1) * $pagesize;
$page. = ', ';
}
if ($num > $pagesize) {
if ($pageval <=1) $pageval = 1;
echo "Total $num".
"Previous page next page";
}
$SQL = "SELECT * from ' message ' Limit $page $pagesize";
$query =mysql_query ($SQL);
while ($row =mysql_fetch_array ($query)) {
?>
Title: |
Time: |
User: |
|
Content: |
}
?>
(4) Final display
Method 3:
Script 2:
Header ("content-type:text/html;charset=gb2312");
$pagesize = 10;
echo $_post[' page ';
$result = mysql_query ("SELECT count (DISTINCT hotelname) from". Tbl_hotels);
$myrow = Mysql_fetch_array ($result);
$numrows = $myrow [0];
$pages =intval ($numrows/$pagesize);
if ($numrows% $pagesize)
$pages + +;
if (Isset ($_post[' page ')) {
$page =intval ($_post[' page ');
}
else{
Set as first page
$page = 1;
}
$first = 1;
$prev = $page-1;
$next = $page +1;
$last = $pages;
Calculate record offset
$offset = $pagesize * ($page-1);
Reads the specified number of records
$result =mysql_query ("Select ' Hotelname ', COUNT (*) from". Tbl_hotels. " GROUP by ' hotelname ' ORDER by id desc limit $offset, $pagesize ");
$num = mysql_num_rows ($result);
while ($row = Mysql_fetch_array ($result, mysql_num)) {
$hotelname [] = $row [0];
$countpeople [] = $row [1];
}
for ($a =0; $a < $num; $a + +)
{
$result =mysql_query ("SELECT count (title) from". Tbl_comments. "Where ' title ' =\" ". $title [$a]." \"");
$row = Mysql_fetch_row ($result);
echo "
\ n "; echo "
\ n"; echo "
\ n "; echo "
\ n "; Rating_bar ($title [$a],5); echo " | \ n "; echo "
$hotelname [$a]\n]; echo " |
\ n "; echo "
\ n "; echo "
| \ n "; echo "
\ n "; echo "Recommended number of people: ($countpeople [$a]) |\n"; echo " mean score: (". $count. ") Tickets) | Number of comments: ()\ n "; echo " |
\ n ";
}
echo "
echo "Border=0>";
echo "";
echo "";
echo "
Section ". $page." Page/Total ". $pages." Page | Total ". $numrows." Bar | ";
if ($page >1) echo "Home |";
if ($page >1) echo "prev |";
if ($page < $pages) echo "next page |";
if ($page < $pages) echo "Last";
echo "Go to PagePage";
echo "
";