Li Depeng Paging and database usage

Source: Internet
Author: User
Tags ord prev string format

index.php

<?php
Include "page.php";


Include "common.php";
Include "db_config.php";

$new =new page (4,1);
//
$sql = "Select Id,username from User";
if (! $sql) {
echo "View Failed";
}
$re =mysql_query ($sql);
if ($re) {
echo "Successful execution";
}
echo "<table border= ' 1 ' width= ' >";
echo "<caption>echo "<tr><th> ID </th> <th> username</th> </tr>";
while (list ($id, $username) =mysql_fetch_row ($re)) {
echo "<tr> <td> $id </td><td> $username </td> </tr>";

# code ...
}
echo "<tr><td colspan= ' 2 ' align= ' right ' >". $new->fpage (). "</tr>";


echo "</table>";

page.php

<?php
Header ("Content-type:text/html;charset=utf-8");

/**
file:page.class.php
Perfect Paging Class page
*/
Class Page {
Private $total; Total number of records in the data table
Private $listRows; Number of rows displayed per page
Private $limit; The SQL statement uses the LIMIT clause to restrict the number of records fetched
Private $uri; Get URL Request address automatically
Private $pageNum; Total pages
private $page;//Current page
Private $config = Array (
' Head ' = ' record ',
' Prev ' = ' prev ',
' Next ' = ' next page ',
' First ' = ' home ',
' Last ' = ' End '
); Display content in the paging information, you can set it yourself through the set () method
Private $listNum = 10; The number of default page list displays

/**
Construction method, you can set the properties of the paging class
Total number of records @paramint $total calculation of paging
@paramint $listRows Optional, set the number of records to display per page by default of 25
@parammixed $query Optional, to pass parameters to the target page, either an array or a query string format
@param bool$ord Optional, the default value is true, the page is displayed from the first page, False is the last page
*/
Public function __construct ($total, $listRows =25, $query = "", $ord =true) {
$this->total = $total;
$this->listrows = $listRows;
$this->uri = $this->geturi ($query);
$this->pagenum = ceil ($this->total/$this->listrows);
/* The following judgment is used to set the current face */
if (!empty ($_get["page"])) {
$page = $_get["page"];
}else{
if ($ord)
$page = 1;
Else
$page = $this->pagenum;
}

if ($total > 0) {
if (Preg_match ('/\d/', $page)) {
$this->page = 1;
}else{
$this->page = $page;
}
}else{
$this->page = 0;
}

$this->limit = "Limit". $this->setlimit ();
}

/**
is used to set the display of paging information for coherent operation
@paramstring $param is the subscript for the member Property array config
@paramstring $value to set the element value for the config subscript The
@returnobject return this object to its own $this, for use in a habitual operation
*/
Function set ($param, $value) {
if (array_key_exists ($param, $this- >config) {
$this->config[$param] = $value;
}
return $this;
}

/* is not a direct call, this method allows you to use the value of the limit and page directly outside of the object to get the private member property, */
Function __get ($args) {
if ($args = = "Limit" | | $args = = "page")
return $this, $args;
else
return null;
}

/**
Output paging in specified format
@paramint0 7 numbers as parameters for customizing the output paging structure and order of the structure, default output all structure
@returnstring paging information content
*/
Function Fpage () {
$arr = Func_get_args ();

$html [0] = "&nbsp; total <b> {$this->total} </b>{$this->config[" Head "]}&nbsp;";
$html [1] = "&nbsp; page <b>". $this->disnum (). " </b> Bar &nbsp; ";
$html [2] = "&nbsp; this page from <b>{$this->start ()}-{$this->end ()}</b> strip &nbsp;";
$html [3] = "&nbsp;<b>{$this->page}/{$this->pagenum}</b> page &nbsp;";
$html [4] = $this->firstprev ();
$html [5] = $this->pagelist ();
$html [6] = $this->nextlast ();
$html [7] = $this->gopage ();

$fpage = ' <div style= ' font:12px \ ' \5b8b\4f53\ ', San-serif; " > ';
if (count ($arr) < 1)
$arr = Array (0, 1,2,3,4,5,6,7);

for ($i = 0; $i < count ($arr); $i + +)
$fpage. = $html [$arr [$i]];

$fpage. = ' </div> ';
return $fpage;
}

/* Private method used inside the object, */
Private Function Setlimit () {
if ($this->page > 0)
Return ($this->page-1) * $this->listrows. ", {$this->listrows}";
Else
return 0;
}

/* Private method used inside the object to get the current URL of the access automatically */
Private Function GetURI ($query) {
$request _uri = $_server["Request_uri"];
$url = Strstr ($request _uri, '? ')? $request _uri: $request _uri. '? ';

if (Is_array ($query))
$url. = Http_build_query ($query);
else if ($query! = "")
$url. = "&". Trim ($query, "?&");

$arr = Parse_url ($url);

if (Isset ($arr ["Query"])) {
Parse_str ($arr ["Query"], $arrs);
unset ($arrs ["page"]);
$url = $arr ["path"]. '? '. Http_build_query ($ARRS);
}

if (Strstr ($url, '? ')) {
if (substr ($url,-1)! = '? ')
$url = $url. ' & ';
}else{
$url = $url. '? ';
}

return $url;
}

/* Private method used inside the object to get the number of records starting at the current page */
Private Function Start () {
if ($this->total = = 0)
return 0;
Else
Return ($this->page-1) * $this->listrows+1;
}

/* Private method used inside the object to get the number of records at the end of the current page */
Private Function End () {
return min ($this->page * $this->listrows, $this->total);
}

/* Private method used inside the object to get operation information for the previous and first page */
Private Function Firstprev () {
if ($this->page > 1) {
$str = "&nbsp;<a href= ' {$this->uri}page=1 ' >{$this->config[" First "]}</a>&nbsp;";
$str. = "<a href= ' {$this->uri}page=". ($this->page-1). "' >{$this->config["prev"]}</a>&nbsp; ";
return $str;
}

}

/* Private method used inside the object to get the page number table information */
Private Function PageList () {
$linkPage = "&nbsp;<b>";

$inum = Floor ($this->LISTNUM/2);
/* list before the current page */
for ($i = $inum; $i >= 1; $i-) {
$page = $this->page-$i;

if ($page >= 1)
$linkPage. = "<a href= ' {$this->uri}page={$page} ' >{$page}</a>&nbsp;";
}
/* Information on current page */
if ($this->pagenum > 1)
$linkPage. = "<span style= ' padding:1px 2px;background: #BBB; Color:white ' >{$this->page}</span>& nbsp; ";

/* list following the current page */
for ($i =1; $i <= $inum; $i + +) {
$page = $this->page+ $i;
if ($page <= $this->pagenum)
$linkPage. = "<a href= ' {$this->uri}page={$page} ' >{$page}</a>&nbsp;";
Else
Break
}
$linkPage. = ' </b> ';
return $linkPage;
}

/* Private method used inside the object to get operation information for the next and last page */
Private Function Nextlast () {
if ($this->page! = $this->pagenum) {
$str = "&nbsp;<a href= ' {$this->uri}page=". ($this->page+1). "' >{$this->config["Next"]}</a>&nbsp; ";
$str. = "&nbsp;<a href= ' {$this->uri}page=". ($this->pagenum). "' >{$this->config["last"]}</a>&nbsp; ";
return $str;
}
}

/* Private method used inside the object to display and process the form jump page */
Private Function Gopage () {
if ($this->pagenum > 1) {
Return ' &nbsp;<input style= ' width:20px;height:17px!important;height:18px;border:1px solid #CCCCCC; "Type=" Text "onkeydown=" Javascript:if (event.keycode==13) {var page= (this.value> '. $this->pagenum. ')? '. $this->pagenum ': this.value;location=\ '. $this->uri. ' Page=\ ' +page+\ '} "value=" '. $this->page. ' " ><input style= "cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type= "button" value= "GO" onclick= "Javascript:var page= (this.previoussibling.value> '. $this->pagenum. ')? '. $this->pagenum ': this.previoussibling.value;location=\ '. $this->uri. ' Page=\ ' +page+\ ' >&nbsp; ';
}
}

/* Private method used inside the object to get the number of record bars displayed on this page */
Private Function Disnum () {
if ($this->total > 0) {
return $this->end ()-$this->start () +1;
}else{
return 0;
}
}
}


comon.ph

<?php
Header ("Content-type:text/html;charset=utf-8");
?>

db_config.php

<?php
mysql_connect ("localhost", "root", "") or Die ("Connection failed");
mysql_select_db ("test") or Die ("Select Failed");

Li Depeng Paging and database usage

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.