PHP5 MySQL pagination instance code _php tips

Source: Internet
Author: User
Tags prev

Copy Code code as follows:

<?php
Connecting to a database
$db =mysql_connect ("localhost", "root", "");
mysql_select_db ("Hy", $db);
Set the number of records to display on each page
$pagesize = 25;
Get the total number of records, calculate total pages with
$res =mysql_query ("SELECT count (*) from main", $db);
$myrow = Mysql_fetch_array ($res);
$numrows = $myrow [0];
Calculate Total Pages
$pages =intval ($numrows/$pagesize);
if ($numrows% $pagesize)
$pages + +;
Determine whether the page is set or not, such as no definition of the homepage
if (!isset ($page))
$page = 1;
Judge go to Page
if (Isset ($ys))
if ($ys > $pages)
$page = $pages;
Else
$page = $ys;
Calculate record offset
$offset = $pagesize * ($page-1);
Take a record
$res =mysql_query ("Select Id,title from main order by id desc limit $offset, $pagesize", $db);
Loop display of records
if ($myrow = mysql_fetch_array ($res))
{
$i = 0;
?>
<table width= "101%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
&LT;TD width= "5%" bgcolor= "#E1E9FB" ></td>
&LT;TD width= "bgcolor=" "#E1E9FB" ><font color= "#FF6666" size= "2" > Content </font></td>
</tr>
<?php
do {
$i + +;
?>
<tr>
&LT;TD width= "5%" bgcolor= "#E6F2FF" ><?php echo $i;? ></td>
&LT;TD width= "bgcolor=" "#E6F2FF" ><font size= "2" >
<a href= "Javascript:popwin" (' view.php?id=<?php echo $myrow [0];? > ') "><?php echo $myrow [1];? ></a></font></td>
</tr>
<?php
}
while ($myrow = Mysql_fetch_array ($res));
echo "</table>";
}
Show Total Pages
echo "<div align= ' center ' > common". $pages. " Page (". $page." /". $pages.") <br> ";
Show pagination
for ($i =1; $i < $page; $i + +)
echo "<a href= ' fenye.php?page=". $i. "' > ". $i." Page </a> ";
echo "section" $page. " Page ";
for ($i = $page +1; $i <= $pages; $i + +)
echo "<a href= ' fenye.php?page=". $i. "' > ". $i." Page </a> ";

echo "<br>";
Show go to Page
echo "<form action= ' fenye.php ' method= ' post ' >";
Calculates the page value of the first page, previous, Next, and last pages
$first = 1;
$prev = $page-1;
$next = $page +1;
$last = $pages;
if ($page >1)
{
echo "<a href= ' fenye.php?page=". $first. "' > Home </a> ";
echo "<a href= ' fenye.php?page=". $prev. "' > Prev </a> ";
}
if ($page < $pages)
{
echo "<a href= ' fenye.php?page=". $next. "' > next page </a> ";
echo "<a href= ' fenye.php?page=". $last. "' > Last </a> ";
}
echo "Go to <input type=text name= ' ys ' size= ' 2 ' value=". $page. " > page ";
echo "<input type=submit name= ' submit ' value= ' Go ' >";
echo "</form>";
echo "</div>";

?>

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.