Home Backend Development PHP Tutorial An example of php mysql template paging class_PHP tutorial

An example of php mysql template paging class_PHP tutorial

Jul 13, 2016 pm 04:54 PM
mysql php Pagination Example Tutorial database method template of kind

/*
* 模板分页类,源于easp教程的数据库教程分页方法,算是easp分页的的php独立版
* 支持动态和静态分页方式
* easp官网http://easp.lengshi.com/
* 作者:钟晶晶
* 日期:2010-11-3
* 邮箱:zhongjingjing@gmail.com
* 博客:http://blog.zaimer.com
* page([总记录数=1],[分页大小=20],[当前页=1],[显示页数=6],[分页参数='page'],[分页链接=当前页面],[是否静态=false])
* 动态:
* 简单用法:
* $page = new page(50);
* $page->setpager('zjj','
共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页
',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
 * echo $page->getpager('zjj');
 * 全参数用法:
 * $page = new page(50,20,1,6,'page','prrr.php',false);
 * $page->setpager('zjj','
共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页
',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
 * echo $page->getpager('zjj');
 * 静态:
 * $page = new page(50,20,1,6,'page','prrr{page}.html',true);
 * $page->setpager('zjj','
共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页
',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
 * echo $page->getpager('zjj');
 */
class page {
 private $page_size; //每页显示的条目数
 private $total_size; //总条目数
 private $current_page; //当前被选中的页
 private $sub_pages; //每次显示的页数
 private $total_pages; //总页数
 private $page_tpl = array (); // 分页模板
 private $pageparam;
 private $pagelink;
 private $static;
 
 function __construct($total_size = 1, $page_size = 20, $current_page = 1, $sub_pages = 6, $pageparam = 'page', $pagelink = '', $static = false) {
  $this->page_size = intval ( $page_size );
  $this->total_size = intval ( $total_size );
  if (! $current_page) {
   $this->current_page = 1;
  } else {
   $this->current_page = intval ( $current_page );
  }
  $this->total_pages = ceil ( $total_size / $page_size );
  $this->sub_pages = intval ( $sub_pages );
  $this->pageparam = $pageparam;
  $this->pagelink = (empty ( $pagelink ) ? $_server ["php_self"] : $pagelink);
  $this->static = $static;
  $this->page_tpl ['default'] = array ('tpl' => '
{first}{prev}{liststart}{list}{listend}{next}{last} 跳转到{jump}页
', 'config' => array () );

}
public function __set($param, $value) {
$this->$param = $value;
}
public function __get($param) {
Return $this->$param;
}
/*
__destruct destructor is called when the class is no longer in use. This function is used to release resources.
*/
function __destruct() {
unset ($page_size); //Number of items displayed on each page
unset ($total_size); //Total number of entries
unset ($current_page); //The currently selected page
unset ( $sub_pages ); //The number of pages displayed each time
unset ( $total_pages ); //Total number of pages
unset ( $page_tpl ); // paging template
unset ($pageparam); //Paging parameters, default page
unset ( $pagelink );
unset ( $static );
}
private function urlparameters($url = array()) {
foreach ( $url as $key => $val ) {
if ($key != $this->pageparam)
$arg [] = $key . '=' . $val;
}
$arg [] = $this->pageparam . '=*';
if ($this->static)
Return str_replace ( '{page}', '*', $this->pagelink );
else
Return $this->pagelink . '?' . implode ( '&', $arg );
}
public function setpager($tpl_name = 'default', $tpl = '', $config = array()) {
if (empty ( $tpl ))
$tpl = $this->page_tpl ['default'] ['tpl'];
if (empty ( $config ))
$config = $this->page_tpl ['default'] ['config'];
$this->page_tpl [$tpl_name] = array ('tpl' => $tpl, 'config' => $config );
}
public function getpager($tpl_name = 'default') {
$this->getcurrentpage ();
Return $this->pager ( $this->page_tpl [$tpl_name] );
}
public function getcurrentpage() {
$this->current_page = ($_get [$this->pageparam] <= intval ( $this->total_pages ) ? ($_get [$this->pageparam] < 1 ? 1 : $_get [$this->pageparam]) : intval ( $this->total_pages ));
}
public function pager($page_tpl = '') {
if (empty ( $page_tpl ))
$page_tpl = $this->page_tpl ['default'];
$cfg = array ('recordcount' => intval ( $this->total_size ), 'pageindex' => intval ( $this->current_page ), 'pagecount' => intval ( $this-> total_pages ), 'pagesize' => intval ( $this->page_size ), 'listlong' => intval ( $this->sub_pages ), 'listsidelong' => 2, 'list' => ' *', 'currentclass' => 'current', 'link' => $this->urlparameters ( $_get ), 'first' => '«', 'prev' => ' 8249;', 'next' => '›', 'last' => '»', 'more' => '...', 'disabledclass' => 'disabled', 'jump' => 'input', 'jumpplus' => '', 'jumpaction' => '', 'jumplong' => 50 );
if (! empty ( $page_tpl ['config'] )) {
foreach ( $page_tpl ['config'] as $key => $val ) {
If (array_key_exists ( $key, $cfg ))
$cfg [$key] = $val;
}
}
$tmps tutorial tr = $page_tpl ['tpl'];
$pstart = $cfg ['pageindex'] - (($cfg ['listlong'] / 2) + ($cfg ['listlong'] % 2)) + 1;
$pend = $cfg ['pageindex'] + $cfg ['listlong'] / 2;
if ($pstart < 1) {
$pstart = 1;
$pend = $cfg ['listlong'];
}
if ($pend > $cfg ['pagecount']) {
$pstart = $cfg ['pagecount'] - $cfg ['listlong'] + 1;
$pend = $cfg ['pagecount'];
}
if ($pstart < 1)
$pstart = 1;
for($i = $pstart; $i <= $pend; $i ++) {
If ($i == $cfg ['pageindex'])
$plist .= '' . str_replace ( '*', $i, $cfg ['list'] ) . ' else
$plist .= ' ' . str_replace ( '*', $i, $cfg [' list'] ) . ' ';
}
  if ($cfg ['listsidelong'] > 0) {
   if ($cfg ['listsidelong'] < $pstart) {
    for($i = 1; $i <= $cfg ['listsidelong']; $i ++) {
     $pliststart .= '' . str_replace ( '*', $i, $cfg ['list'] ) . ' ';
    }
    $pliststart .= ($cfg ['listsidelong'] + 1) == $pstart ? '' : $cfg ['more'] . ' ';
   } else {
    if ($cfg ['listsidelong'] >= $pstart && $pstart > 1) {
     for($i = 1; $i <= ($pstart - 1); $i ++) {
      $pliststart .= '' . str_replace ( '*', $i, $cfg ['list'] ) . ' ';
     }
    }
   }
   if (($cfg ['pagecount'] - $cfg ['listsidelong']) > $pend) {
    $plistend = ' ' . $cfg ['more'] . $plistend;
    for($i = (($cfg ['pagecount'] - $cfg ['listsidelong']) + 1); $i <= $cfg ['pagecount']; $i ++) {
     $plistend .= ' ' . str_replace ( '*', $i, $cfg ['list'] ) . ' ';
    }
   } else {
    if (($cfg ['pagecount'] - $cfg ['listsidelong']) <= $pend && $pend < $cfg ['pagecount']) {
     for($i = ($pend + 1); $i <= $cfg ['pagecount']; $i ++) {
      $plistend .= ' ' . str_replace ( '*', $i, $cfg ['list'] ) . ' ';
     }
    }
   }
  }
  if ($cfg ['pageindex'] > 1) {
   $pfirst = ' ' . $cfg ['first'] . ' ';
   $pprev = ' ' . $cfg ['prev'] . ' ';
  } else {
   $pfirst = ' ' . $cfg ['first'] . ' ';
   $pprev = ' ' . $cfg ['prev'] . ' ';
  }
  if ($cfg ['pageindex'] < $cfg ['pagecount']) {
   $plast = ' ' . $cfg ['last'] . ' ';
   $pnext = ' ' . $cfg ['next'] . ' ';
  } else {
   $plast = ' ' . $cfg ['last'] . ' ';
   $pnext = ' ' . $cfg ['next'] . ' ';
  }
  switch (strtolower ( $cfg ['jump'] )) {
   case 'input' :
    $pjumpvalue = 'this.value';
    $pjump = '     $pjump .= ' onkeydown="网页特效:if(event.charcode==13||event.keycode==13){if(!isnan(' . $pjumpvalue . ')){';
    $pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
    $pjump .= '}return false;}" />';
    break;
   case 'select' :
    $pjumpvalue = "this.options[this.selectedindex].value";
    $pjump = '';
    break;
  }
  $patterns = array ('/{recordcount}/', '/{pagecount}/', '/{pageindex}/', '/{pagesize}/', '/{list}/', '/{liststart}/', '/{listend}/', '/{first}/', '/{prev}/', '/{next}/', '/{last}/', '/{jump}/' );
  $replace = array ($cfg ['recordcount'], $cfg ['pagecount'], $cfg ['pageindex'], $cfg ['pagesize'], $plist, $pliststart, $plistend, $pfirst, $pprev, $pnext, $plast, $pjump );
  $tmpstr = chr ( 13 ) . chr ( 10 ) . preg_replace ( $patterns, $replace, $tmpstr ) . chr ( 13 ) . chr ( 10 );
  unset ( $cfg );
  return $tmpstr;
 }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631735.htmlTechArticle?php教程 /* * 模板分页类,源于easp教程的数据库教程分页方法,算是easp分页的的php独立版 * 支持动态和静态分页方式 * easp官网http://easp.leng...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What are the advantages of using MySQL over other relational databases? What are the advantages of using MySQL over other relational databases? May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How to analyze the execution plan of MySQL query How to analyze the execution plan of MySQL query Apr 29, 2025 pm 04:12 PM

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

See all articles