Home>

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

Method 1:Speaking of SQL queries for paging,Need to call several functions,See the script:1.pager.class.php

<?php
  class pager {
    public $sql;//sql query
    public $datanum;//Query the total number of all data records
    public $page_size;//Number of records displayed per page
    protected $_errstr;
    protected $_conn;
    protected $_query_id;
    public function query ($query) ///There is a problem with this function,Not necessary for now
    {
    $ret=false;
    if (! empty ($query)) {
      if ($this->_conn === false ||! is_resource ($this->_conn)) {
       warninglog (__ method__. ":query sql with no connection", true);
      return false;
      }
    $this->[email protected]_query ($query, $this-&_ conn);
    if ($this->_query_id === false) {
    $this->[email protected]_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 number
    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);
      }
    }
//Offset of database query
    public function start () {
      return ($this->page_id ()-1) * $this->page_size;
    }
    //Data output
    public function sqlquery () {
      return $this->sql. "limit". $this-&start;start (). ",". $this->page_size;
    }
    //Get the current file name
    private function php_self () {
      return $_server ["php_self"];
    }
    //Previous page
    private function pre_page () {
      if ($this->page_id () == 1) {//Number of pages equals 1
        return "&a href =". $this->php_self (). "?page_id=1". $this->url (). ">Previous</a>
      } elseif ($this->page_id ()!=1) {//The number of pages is not equal to 1
        return "&a href =". $this->php_self (). "?page_id =". ($this->page_id ()-1). $this-&url;url (). ">previous</a>";
      }
    }
    //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 ++)
          $display_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 & lt;= 10;$i ++) //Display the page in a loop
            $display_page.="&a;"</a>";
            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 ++) //show the page in a loop
            $display_page.="&a;"</a>";
 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 ++)
            $display_page.="&a href =". $this->php_self (). "?page_id =". $i. $this->url (). ">". $i. "</a>";
            return $display_page;
        }
      }
    }
    //Next page
    private function next_page () {
      if ($this->page_id ()<$this->page_num ()) {//The number of pages is less than the total number of pages
        return "&a href =". $this->php_self (). "?page_id =". ($this->page_id () + 1). $this-&url;url (). ">Next page</a>";
      } elseif ($this->page_id () == $this->page_num ()) {//The number of pages is equal to the total number of pages
        return "&a href =". $this->php_self (). "?page_id =". $this->page_num (). $this-&url;url (). ">Next Page</a>";
      }
    }
    //Set paging information
    public function set_page_info () {
      $page_info="Total". $this->datanum. "items";
      $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;url (). "> ;Last page</a>";
      $page_info.="section". $this-&page;id _) ("."/$. this->page_num (). "page";
      return $page_info;
    }
  }
?>

2. Script 2:

<?php
  //class usage
  //read 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,And get 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"]. "<br />";
  }
  //page number index bar
  echo $page->set_page_info ();
?>

Method 2:Use the Ajax method

1.First understand the limit usage in SQL statements

select * from table ...... limit starting position, number of operations (where the starting position starts from 0)

example

Take the first 20 records:select * from table ...... limit 0, 20

Take 20 records starting from Article 11:select * from table ...... limit 10, 20

limit n is equivalent to limit 0, n.

Such asselect * from table limit 5;//return the first 5 rows,andselect * from table limit 0, 5Same as

2.Paging principle

The so-called pagination display,That is, the result set in the database,Show it piece by piece

How to segment,Currently in paragraphs (a few per page,Current page number)

First 10 records:select * from table limit 0,10Records 11 to 20:select * from table limit 10,10Records 21 to 30:select * from table limit 20,10Pagination formula:

(Current page-1) x number of pages per page, number of pages per page

select * from table limit ($page- 1) * $pagesize, $pagesize

3. $_server ["request_uri"] functionOne of the predefined server variables,Everything starting with $_server is called a scheduled server variable.

The role of request_uri is to get the current uri, that is, 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 functionparse_url () is a function that parses a URL into an array with a fixed key value

example

$ua=parse_url ("http://username:[email protected]/path?arg=value #anchor");
print_r ($ua);

result:

array
(
 [scheme] =>http;protocol
 [host] =>hostname;host domain name
 [user] =>username;user
 [pass] =>password
 [path] =>/path;path
 [query] =>arg=value;take parameters
 [fragment] =>anchor;
)

5, code examplesThis one-note tab,Divided into 3 parts, one is database design,One is the connection page,One is the display page.

(1) Design the database

The design database is named bbs, and there is a data table named message, which contains fields such as title, lastdate, user, content, etc., which respectively indicate the title of the message.A few days ago,Messager, content of message

(2) Connection page

<?php
$conn [email protected]_connect ("localhost", "root", "123456") or die ("Database link error");
mysql_select_db ("bbs", $conn);
mysql_query ("set names" gbk "");//use gbk Chinese encoding;
//Convert spaces and newlines to html parsable
function htmtocode ($content) {
 $content=str_replace ("\ n", "<br>", str_replace ("", "", $content));//Two str_replace nested
 return $content;
}
//$content=str_replace ("" "," "", $content);
 //htmlspecialchars ();
?>

(3) Display the 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                       					
				
					
php
  • Previous Example of simple percentage progress bar effect implemented by jQuery
  • Next jQuery Ajax display loading animation asynchronously while loading data