PHP 3 ways to achieve Paging function 1th 3 page, Page 3 page _php tutorial

Source: Internet
Author: User

PHP 3 ways to achieve Paging function 1th 3 page, Page 3 page


Directly on the code, I hope you read it carefully.

Method One: To talk about SQL query paging, you need to call several functions, see script:
1.pager.class.php

<?php class Pager {public $sql;//sql query Statement public $datanum;//query all data total records public $page _size;//per page show the number of records    protected $_errstr;    protected $_conn;    protected $_query_id;    Public Function query ($query)///This function has a problem and can be used temporarily without {$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-&GT;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);    }}//database query offset 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 ']; }//prev 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 paging Private Function Display_page () {$display _page = ""; if ($this->page_num () <= 10) {//less than 10 pages for ($i =1; $i <= $this->page_num (); $i + +)//Cycle Display 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 + +) The loop shows the 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 = $thi S-&GT;PAGE_ID ()-5; $i <= $this->page_id () +4; $i + +)//cycle shows the 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 shows the page $display _page. = "Php_self ()."? Page_id= ". $i. $this->url ()." > ". $i."            ";        return $display _page; }}}//Next Private Function Next_page () {if ($this->page_id () < $this->page_num ()) {//page less than total Page return "Php_self ()."? Page_id= ". ($this->page_id () +1). $this->url (). "      > next Page "; }elseif ($this->page_id () = = $this->page_num ()) {//pages equals the total number of 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:

<?php  //Class usage  //Read the 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 result  $sql = "Select Word from ufeature.spam_accuse_word_list where flag= ' 0";  Paging initialization  $page = new Pager ($sql, p);  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 strip echo $page->set_page_info ();? >

Method Two: 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;//Return the first 5 rows, and 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

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");p Rint_r ($ua);

Results:

Array ([scheme] + HTTP  ; protocol [host] + hostname  ; host domain [user] = username  ; user [pass] = password
  

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

<?php$conn = @ mysql_connect ("localhost", "root", "123456") or Die ("Database link Error"); mysql_select_db ("BBS", $conn); mysql_ Query ("Set names ' GBK '"); Use GBK Chinese encoding;//To convert a space, newline to HTML resolvable function Htmtocode ($content) {$content = Str_replace ("\ n", "
", Str_replace (" "," ", $content)); Two str_replace nested return $content;} $content =str_replace ("'", "'", $content); Htmlspecialchars ();?>

(3) Display page

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

http://www.bkjia.com/phpjc/1067830.html www.bkjia.com true http://www.bkjia.com/phpjc/1067830.html techarticle php 3 ways to achieve Paging function 1th 3 page, Page 3 page directly on the code, I hope you read it carefully. Method One: To talk about SQL query paging, you need to call several functions, see ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.