Use Php+mysql to do a paginated presentation

Source: Internet
Author: User
Tags html tags rowcount

Use PHP to make a page to understand the logical relationship is not difficult, but I was in the lecture is estimated by the teacher said those variables in the English words to listen to, because there are a few variables of the name are very similar, just changed two or three English letters, some fewer such, Heard half already do not know what the teacher said this variable represents, write this is just to review, warm so know new.

Page: Pages per: By, rely on

//reference database a wrapper function with a database attached to it uses the My_query () function when using SQL statements; include./mysqldb.php;

//Set the number of pages currently selected                         
$pageNum = isset ($_get[' num ')? $_get[' num ']: 1;
//define the number of records to display per page
$rowsPerPage = 5;
//Query total record Count
$sql = "SELECT count (*) from publish";
$result = My_query ($sql);
$rows = Mysql_fetch_row ($result);
$rowCount = $rows [0];//Total Record Count
//Calculate total pages
$pages = Ceil ($rowCount/$rowsPerPage); Total pages = rounded up (total number of records/number of records displayed per page)
//Piece together page number string
$strPage = "; This is to define an empty string, and then concatenate the HTML tags and contents in a concatenated fashion.
//Put together first pages first
$strPage. = "<a href= './list_father.php?num=1 ' > Home </a>";
//Piece up the previous page
$preNum = $pageNum = = 1? 1: $pageNum-1;
$strPage. = "<a href= './list_father.php?num= $preNum ' > Prev </a>";
//Display the initial page
if ($pageNum <= 3) {
$startNum = 1;
}else{
$startNum = $pageNum-2;
}
//Display the initial page maximum value
if ($startNum >= $pageNum-4) {//To display five pages so the maximum value of the initial page cannot be greater than the current page-4
$startNum = $pageNum-4;
}
//Prevent negative numbers from appearing
if ($startNum <=1) {
$startNum = 1;
}
//Determine the last page displayed $endnum
$endNum = $startNum + 4; To display five pages so the last page must be the initial page +4
//Prevent the last page from being out of bounds
if ($endNum >= $pages) {//The last page is larger than the total page let's make him the number of the total page
$endNum = $pages;
}
for ($i = $startNum; $i <= $endNum; $i + +) {//Take the initial page and the last page cycle than cycle, loop 5 times
if ($i = = $pageNum) {//The current number of pages is selected to give it a style to distinguish the number of pages on another page
$strPage. = "<a href= './list_father.php?num= $i ' ><font color=blue> $i </font></a>";
}else{
$strPage. = "<a href= './list_father.php?num= $i ' > $i </a>";
}
}
//Piece together the next page
$nextNum = $pageNum = = $pages? $pages: $pageNum +1;
$strPage. = "<a href= './list_father.php?num= $nextNum ' > Next </a>";
//Piece together the last page
$strPage. = "<a href= './list_father.php?num= $pages ' > Last </a>";
//pagination to this end

<?php

function My_connect ($arr) {
//Accept Parameters
$host = Isset ($arr [' Host '])? $arr [' Host ']: 'Host name ';
$port = Isset ($arr [' $port '])? $arr [' Port ']: 'Port Number ';
$user = Isset ($arr [' user '])? $arr [' User ']: 'user type ';
$pass = Isset ($arr [' isset '])? $arr [' isset ']: 'self-setting password ';
$link = @ mysql_connect ("$host: $port", $user, $pass);
if (! $link) {
//Database connection failed
echo "Database connection failed! <br/> ";
echo "Error number:", Mysql_errno (), "<br/>";
echo "error message:", Mysql_error (), "<br/>";
Die
}
}
function My_query ($sql) {
//Execute SQL statement first
$result = mysql_query ($sql);
//Determine if the execution succeeds
if (! $result) {
//Output related error message
echo "SQL statement execution failed! <br/> ";
echo "Error SQL statement is:", $sql, "<br/>";
echo "Error number:", Mysql_errno (), "<br/>";
echo "error message:", Mysql_error (), "<br/>";
Die
}
return $result;
}
//sets the default character set
function My_charset ($charset) {
$sql = "Set names $charset";
My_query ($sql);
}
//Set default database
function My_database ($db _name) {
$sql = "Use $db _name";
My_query ($sql);
}
//Connect to database
$arr = Array (
"User" and "="User Type",
"Pass" = "Set the password"
);
//Connect Data Trilogy
My_connect ($arr);
My_charset ('Character Set ');
My_database ('the database to select ');

Use Php+mysql to do a paginated presentation

Related Article

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.