PHP function implementation pagination with text paging and digital paging _php skills

Source: Internet
Author: User
Tags error handling

Most recently, you want to use pagination in your project. Paging is a feature that is often used, so it is encapsulated in functional form.

Paging/** * $pageType Paging Type 1 is a digital paging 2 is a text paging * can pass $pagetotal, $page, $total and other data as parameters, or in paging as a global variable (recommended)/function paging ($ PageType) {Global $pageTotal, $page, $total; if ($pageType = = 1) {echo ' <div id= ' pagenum ' > Echo ' <ul> ' for ($i = 0; $i < $pageTotal; $i + +) {if ($page = = ($i + 1)) {echo ' <li><a href= ' blogfriends.php?page= '. $i + 1). "Class=" selected ">". ($i + 1). '
</a></li> '; else {echo ' <li><a href= ' blogfriends.php?page= '. ( $i + 1). ' " > '. ($i + 1). '
</a></li> ';
} echo ' </ul> ';

Echo ' </div> '; else if ($pageType = = 2) {echo ' <div id= ' pagetext ' > '; echo ' <ul> '; Echo ' <li> '. $page. ' /'. $pageTotal. ' Page |
</li> '; Echo ' <li> shared <strong> '. $total. ' </strong> Member |
</li> '; First if ($page = = 1) {echo ' <li> home | </li> '; echo ' <li> previous page | </li> ';} else {//$_server[' SCRIP T_name "] Gets the current script name, facilitates porting//can also customize constants, constant values and script file names consistent with Echo ' <li><a href= '. $_server[script_naME "]. '" > Home </a>|
</li> '; Echo ' <li><a href= '. $_server["Script_name". Page= '. ($page-1). ' " > Previous Page </a>|
</li> '; //Last page if ($page = = $pageTotal) {echo ' <li> next | </li> '; echo ' <li> last page | </li> ';} else {echo ' <li><a href= "'. $_server[" Script_name ". Page= '. ($page + 1). ' " > next Page </a>|
</li> '; Echo ' <li><a href= '. $_server["Script_name". Page= '. ($pageTotal). " > Last </a>|
</li> ';
Echo ' </ul> ';
Echo ' </div> ';

 }
}

Parameter explanation:

$pageTotal is the total number of pages, $page is the current page, $total is the total amount of data obtained from the database;

To simplify, encapsulate all of the parameters

Paging parameter
/**
* $sql An SQL statement that can get the total number of data
* $size display the number of bars per page
/function Pageparam ($sql, $size)
{
//Set global variables for all parameters involved//
$pagestart where a page starts
//$total Total Records $page a page $pageTotal total pages
global $pagestart, $ PageSize, $total, $page, $pageTotal;
$pagesize = $size;
Get Total data
$total = mysql_num_rows (Querydb ($sql));

Error handling, first to determine if there is
an if (isset ($_get[' page '))
{
//specific page
$page = $_get[' page '];
Determine if NULL (0 is NULL)/less than 0/is a number if
(Empty ($page) | | $page < 0 | |!is_numeric ($page))
{
$page = 1;
}
else
{
$page = intval ($page);//rounding to prevent decimal occurrences
}

}
else
{
//Initialize display 1th page
$page = 1;

//Database Clear 0
if ($total = = 0)
{
//set to 1
$pageTotal = 1;
}
The total number of pages of else {//pagination (in-one rounding)
$pageTotal = ceil ($total/$pagesize);
}

The number of pages is greater than the total page $total
if ($page > $pageTotal)
{
$page = $pageTotal;
}
When a page starts from a record
$pagestart = ($page-1) * $pagesize;
}

Parameter explanation:

$pagestart is when a page starts from a record, $pagesize is the number of records displayed per page

In use, call Pageparam First, and then call paging

/**
* First SQL statement that can get the total number of data
* The second page shows the number of bars
/Pageparam ("Select userid from User", 2);

<?php 
//Paging type 1 is a digital paging 2 is a text paging
paging (2);
? >

The location of the call is selected according to the specific situation, and the text is paginated as follows:

<?php 
//Paging type 1 is a digital paging 2 is a text paging
paging (1);
? > 

The digital paging is as follows:

The style adjusts itself.

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.