PHP paging a variety of Implementation program code

Source: Internet
Author: User
Tags prev

Paging Code

The code is as follows Copy Code
<?php
Page current page, num pages per page
$page =isset ($_get[' page ')? Intval ($_get[' page '): 1;
$num = 5;
$conn = mysql_connect (' 127.0.0.1 ', ' root ', ');
if (! $conn) {
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db (' Shop ');
Get Total Records
$total =mysql_num_rows (mysql_query ("SELECT * from User"));
Count pages
$pagenum =ceil ($total/$num);
Limit number of pages not exceeding range
If ($page > $pagenum | | $page = = 0) {
Echo ' Error:can not Found the page. '
Exit;
}
The statement "Select * from table limit 0,10" Extracts 10 messages from the table, 0 is the starting point, and 10 is the number of extracts
$offset is the starting value, such as the current page is the first page, 5 per page, the starting point is 0, the same is currently the second page, the starting point will be 5
$offset = ($page-1) * $NUM;
$result =mysql_query ("SELECT * from User limit $offset, $num");
while ($it =mysql_fetch_array ($result)) {
echo ' ID: '. $it [' id ']. ' Name: '. $it [' name ']. ' <br/> ';
}
Show link page toggle link, current Page no link
for ($i =1; $i <= $pagenum; $i + +) {
$show = ($i!= $page)? " <a href= ' index.php?page= '. $i. "' > $i </a> ": <b> $i </b>";
echo $show. " ";
}
echo $total. ' Record, 5 per page, total '. $pagenum. ' Page ';
Mysql_free_result ($result);
Mysql_close ($conn);
?>

Instance 2

The code is as follows Copy Code

?

Function.php here is the main function
function Php_page ($page _dbname, $page _size, $page)
{
if ($page = = "")
{$page = 1;};
if ($LJJL = = "")
{$LJJL = 0;};
if ($page)
{
$page _dbname= "Tb_insert";
$page _size=4; Display 4 records per page
$query = "SELECT count (*) as total from". $page _dbname; Reading data from the database
$result =mysql_query ($query);
$message _count=mysql_result ($result, 0, "total"); Gets the total number of records
$page _count=ceil ($message _count/$page _size); Get total number of pages
$offset = ($page-1) * $page _size;
$query = "SELECT * from". $page _dbname. "desc limit $offset, $page _size";
$result =mysql_query ($query);
The content shows that this is just a simple demonstration, you can change according to your requirements casually.
while ($myrow = @mysql_fetch_array ($result))
{
echo $myrow [name]. "| ". $myrow [number]." | ". $myrow [Tel]." | ". $myrow [Address]." <br><br> ";
}

}
echo "page:". $page. " /";
echo $page _count. " Page record: ";
echo $message _count. " Article "." <br> ";

if ($page!=1)
{
echo "<a href=index.php?page=1> home </a>&nbsp;";
echo "<a href=index.php?page=". ($page-1). " > Prev </a>&nbsp; ";
}
Else
{
echo "Home &nbsp;";
echo "Prev &nbsp;";
}

for ($i =1; $i <= $page _count; $i + +)
{
if ($page = = $i)
{echo $i. " &nbsp; ";}
Else
{echo "<a href=index.php?page= $i > $i </a>&nbsp;";}
}

if ($page < $page _count)
{
echo "<a href=index.php?page=". ($page + 1). " > next page </a>&nbsp; ";
echo "<a href=index.php?page=". $page _count. " > Last </a> ";
}
Else
{
echo "next page &nbsp;";
echo "Last";
}
}
?>

conn.php Connection Database

The code is as follows Copy Code

<?php

$id =mysql_connect ("localhost", "root", "root") or dir (' Connection failed: '. mysql_error ());
Mysql_connect ("IP", "username", "password")

if (mysql_select_db ("db_database06", $id))
echo "";
Else
Echo (' Connection failed: '. mysql_error ());
mysql_query ("Set names gb2312");
?>

Main program, the above two files are included in, and then call a php_page () to complete the paging. :)

The code is as follows Copy Code

<?php

Require_once ("conn.php");
Require_once ("function.php");
Php_page ("Tb_insert", 3,$_get[page]); "Tb_insert" is the table name, 3 is the number of records to display on each page, $_get[page] is the page ID, just use it.
?>

On the one is the text pagination, such as the previous page, the next page and so on, there is a digital paging, like 1,2,3 ... And so on, this is simpler, the former is a little more complicated. The last one is a combination of their 2.

The code is as follows Copy Code

--> 1//Get current page
if (Isset ($_get[' page ')) {
$page = intval ($_get[' page '));
}
else {
$page = 1;
}
$PageSize = 1; Number of records per page
Get Total Quantity
$sql = "SELECT count (*) from blog";
$result = mysql_query ($sql);
$row = Mysql_fetch_row ($result);
$amount = $row [0];
/* Calculate Total Pages
if ($amount) {
if ($amount < $PageSize) {//If the total quantity is less than the number of records per page $pagesize, then there is only one page.
$PageCount = 1;
}
if ($amount% $PageSize) {//Total quantity divided by the number of records per page taken from
$PageCount =& amp;nbsp (int) ($amount/$PageSize) +1;//If so, the number of pages equals the total quantity in addition to the number of records per page plus 1
}
else{
$PageCount =& amp;nbsp; $amount/$PageSize;//No, the result is the number of pages
}
}
else{
$PageCount = 0;
}*/
$PageCount = Ceil ($amount/$PageSize);//Total number of pages = Total quantity divided by the number of pages per page if there are small trees, carry < span style= "color: #008000;" >
if ($Page > $PageCount | $page ==0) {//If the current page is greater than the total number of pages
echo "Cannot find this page!" ";
Exit ();
}

Page links
$PageOut = ';
if ($page ==1) {//If the pages are only one page
$PageOut. = ' first page | prev ';
}
else{
$PageOut. = ' <a href= ' index.php?page=1 ' > first page & lt;/a>|<a href= ' index.php?page= '. ($page-1). ' " > on a page </a>| ';
}
if ($page = = $PageCount | | $PageCount ==0) {//If the current page is equal to the total number
$PageOut. = ' next page | last ';
}
else{
$PageOut. = ' <a href= ' index.php?page= '. ($page + 1). ' " > next page </a>|<a href= "Index.php?page= '. $PageCount." > Last </a> ';
}
Get Data
if ($amount) {
$sql = "SELECT * from Blog limit". ($page-1) * ($PageSize). ", $PageSize";
$result =mysql_query ($sql);
while ($row =mysql_fetch_array ($result)) {//This code is just an example
$blogs [] = array (' Bid ' => $row [' Bid '], ' title ' => $row [' title ']);
foreach ($blogs as $blog) {
$title = $blog [' title '];
}
$output = "<a href=index.php?action=blog_del&bid= $blog [bid]> Delete </a>".
"<a href=index.php?action=blog_edit&bid= $blog [bid]> edit </a>".
"<a href=index.php?action=blog_view&bid= $blog [bid]> view </a>";
Include ("template/default/blog.tpl.php");
Echo $PageOut;
}
for ($i =1; $i <= $PageCount; $i + +) {//Digital paging
$Pageshow = ($i!= $page)? " <a href= ' index.php?page= '. $i. "' > $i </a> ": <b> $i </b>";
Echo $PageShow;
}
echo $amount. ' Record, per page '. $PageSize. ', total '. $PageCount. ' Page ';
}

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.