Home > Backend Development > PHP Tutorial > php paging php paging sample code

php paging php paging sample code

WBOY
Release: 2016-07-29 08:36:45
Original
966 people have browsed it

/*
Requirements: Create a test database and create a test table in it.
As long as the id field is entered, just enter the data. .
Due to limited level, mistakes are inevitable. .
*/
$conn = mysql_connect("localhost","root","");
$maxnum = 2; //Number of records displayed on each page
mysql_select_db("test", $conn);
$query1 = "SELECT COUNT(*) AS totalrows FROM test ";
$result1 = mysql_query($query1, $conn) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
$totalRows1 = $row1[ ' totalrows']; //The total number of data in the data set
$totalpages = ceil($totalRows1/$maxnum);//Calculate the total number of pages that can be paged, ceil() is the rounding function
if(!isset($_GET['page ']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1; //Perform default processing for 3 types of errors
//In the url parameter When page does not exist, when page is not a decimal number, when page is greater than the number of pages that can be divided, the default is 1
else $page = $_GET['page'];
$startnum = ($page - 1)*$ maxnum; //Start from the $startnum item in the data set. Note that the data set starts from 0
$query = "SELECT * FROM test LIMIT $startnum,$maxnum"; //Select data that meets the requirements from $startnum Starting from the data, select $maxnum rows
$result = mysql_query($query, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>

< ;head>
//Implementation << < 1 2 3 4 5> >> Pagination link
$pre = $page - 1;//Previous page
$next = $page + 1;//Next page
$maxpages = 4;//When processing paging << < 1 2 3 4 > >>Display 4 pages
$pagepre = 1;//If the current page is 4, the previous $pagepre page should also be displayed, such as << < 3 /4/ 5 6 > >> 3 pages are displayed
if($page != 1) { echo "<< ";
echo " < ";}
if($maxpages>=$totalpages) / /If the total records are not enough to display 4 pages
{$pgstart = 1;$pgend = $totalpages;}//There will be no printing of all pages
elseif(($page-$pagepre-1+$maxpages)>$ totalpages)//As if the total number of pages is 6 and the current one is 5, the previous 3 4 should be displayed instead of just 4
{$pgstart = $totalpages - $maxpages + 1;$pgend = $totalpages; }
  else{
  $pgstart=(($page<=$pagepre)?1:($page-$pagepre));//When the current page is 1, it will only be 1 2 3 4 > >> Instead of 0 1 2 3 > >>
$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1)); ;$pg<=$pgend;$pg++){ //Jump menu
if($pg == $page) echo "$pg ";
else echo "$pg "; ='.$next."'>> ";
echo "> ";}
?>
                                                                                                                                                                                              ) {                                                                                                                " ;/select>
                                                                        class="tr"> /table>



mysql_free_result($result1);
mysql_free_result($result);
?>

The above introduces the php paging and php paging sample code, including the content of php paging. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template