3 ways to achieve paging functionality in PHP 1th/3 page _php Tips

Source: Internet
Author: User
Tags php class prev

Directly on the code, I hope you read carefully.

Method One: Speaking of 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 the total number of data records public $page _size;//per page display record
    Protected $_errstr of the number of bars;
    protected $_conn;

    protected $_query_id;
    Public Function Query ($query)///This function has a problem, temporarily can not use {$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 pages 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;    The//Data output Public function SQLQuery () {return $this->sql. "Limit". $this->start (). ",". $this->pa
    Ge_size;
    //Get current file name Private function php_self () {return $_server[' php_self ']; //Previous Private Function Pre_page () {if ($this->page_id () = 1) {//page equals 1 return "<a href=". $this->php_self (). "? Page_id=1 ". $this->url ()."
      > Prev </a> "; }elseif ($this->page_id ()!= 1) {//Page not equal to 1 return "<a href=". $this->php_self (). "? Page_id= ". ($this->page_id ()-1). $this->url (). "
      > Prev </a> ";
      }///Show paging Private Function Display_page () {$display _page = ""; if ($this->page_num () <= 10) {//less than 10 for ($i =1; $i <= $this->page_num (); $i + +)//Loop Display page $displa Y_page. = "<a href=". $this->php_self (). "? Page_id= ". $i. $this->url ()." > ". $i."
          </a> ";
      return $display _page;
 }elseif ($this->page_num () > 10) {//greater than 10 pages       if ($this->page_id () <= 6) {for ($i =1; $i <=10; $i + +)//Loop Display page $display _page. = "<a Href= ". $this->php_self ()."? Page_id= ". $i. $this->url ()." > ". $i."
            </a> ";
        return $display _page; }elseif (($this->page_id () > 6) && ($this->page_num ()-$this->page_id () >= 4)) {for ($i = $th IS-&GT;PAGE_ID ()-5; $i <= $this->page_id () +4; $i + +)//loop displays the page $display _page. = "<a href=". $this->php_s Elf (). "? Page_id= ". $i. $this->url ()." > ". $i."
 </a> ";
        return $display _page; }elseif (($this->page_id () > 6) && ($this->page_num ()-$this->page_id () < 4)) {for ($i = $thi S->page_num ()-9; $i <= $this->page_num (); $i + +)//loop displays the page $display _page. = "<a href=". $this->php_se LF (). "? Page_id= ". $i. $this->url ()." > ". $i."
            </a> ";
        return $display _page;
      ()}//Next page Private function Next_page () {if ($this->page_id () < $this->page_num ()) {//page is less than the total number of pages return "<a href=". $this->php_self (). "? Page_id= ". ($this->page_id () +1). $this->url ().
      > next page </a> "; }elseif ($this->page_id () = = $this->page_num ()) {//page number equals total page return "<a href=". $this->php_self (). "? Page_id= ". $this->page_num (). $this->url ()."
      > next page </a> "; }//Set paging information public function Set_page_info () {$page _info = "Total". $this->datanum. "
      Article "; $page _info. = "<a href=". $this->php_self (). "? Page_id=1 ". $this->url ()."
      > Home </a> ";
      $page _info. = $this->pre_page ();
      $page _info. = $this->display_page ();
      $page _info. = $this->next_page (); $page _info. = "<a href=". $this->php_self (). "? Page_id= ". $this->page_num (). $this->url ()."
      > Last </a> "; $page _info. = "a". $this->page_id (). /". $this->page_num ()."
      Page ";
    return $page _info;

 }}?>

2. Script 2:

<?php
  ///
  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 result of the query
  $sql = "Select Word from ufeature.spam_accuse_word_list where flag= ' 0";
  Pagination initialization
  $page = new Pager ($sql);
  20 is the number shown 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]. <br/> ";
  }
  Page number index bar
  echo $page->set_page_info ();


? >

Method Two: Using the Ajax method
1, first understand the SQL statements in the limit usage

SELECT * FROM table ... limit start position, number of operand (where start position is starting from 0)

Example
Take the first 20 records: SELECT * from Table ... limit 0, 20
Starting from 11th, take 20 records: SELECT * FROM Table ... limit 10, 20
LIMIT n is equivalent to LIMIT 0,n.
such as select * from table LIMIT 5;//Return to the first 5 rows, same as 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 show
How to segment, currently in the first few paragraphs (a few on each page, current on a few pages)
First 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-splitting formula:
(Current page-1) X number of pages per page, number of bars per page

Select * FROM table limit ($Page-1) * $PageSize, $PageSize 

3, $_server["Request_uri"] function
One of the predefined server variables, all of which begin with the $_server of the server variable.
The function of Request_uri is to obtain the current URI, in addition to the full address path that follows 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 a fixed key value
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   ; path
 [query] => arg=value  ; fetch Parameters
 [ Fragment] => anchor  ;

5. code example
This is a message of pagination, divided into 3 parts, one is the database design, one is the connection page, one is the display page.
(1) Design Database
Design database named BBS, there is a data table for message, which contains title,lastdate,user,content, and other fields, respectively, indicating the title, message days ago, 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 code;
Converts a space, newline to an HTML resolvable
function Htmtocode ($content) {
 $content = str_replace ("\ n", "<br>", Str_replace (" "," ", $content)); Two str_replace nested return
 $content;
}
$content =str_replace ("'", "'", $content);
 Htmlspecialchars ();
 
? >

(3) Display page

<?php
 include ("conn.php");
$pagesize = 2; Set 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

  
   
   
Current 1/3 page 123 Next read the full text

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.