本文共 6912 字,大约阅读时间需要 23 分钟。
php分页代码简单实现
As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to make it as simple as possible and as short as possible AND so we all can reach for it at EE.
随着本主题以不同形式反复出现,我终于决定写一篇简短的(是的,对的...)有关使用MySQL数据库与PHP分页的文章/教程。 我知道有数十种此类教程-我想使其尽可能简单和尽可能短,因此我们都可以在EE上找到它。
First of all - the preparations. I assume you have your Apache + PHP set up (by XAMPP / WAMP / any other).
首先-准备。 我假设您已经设置了Apache + PHP(通过XAMPP / WAMP /任何其他方式)。
1. What you need is one file - lets name it.... hmmm.... index.php ! Place it under a directory of your choice.
1.您只需要一个文件-命名为... hmmm .... index.php! 将其放在您选择的目录下。
2. Let's setup a database. If you use phpmyadmin, you know what to do - import the script creating the "pagination" table. I have created a database named "test" for it.
2.让我们建立一个数据库。 如果您使用phpmyadmin,则知道该怎么做-导入创建“分页”表的脚本。 我已经为其创建了一个名为“ test”的数据库。
CREATE TABLE IF NOT EXISTS `pagination` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=22 ;
Just the 'id' and 'name' (simple, simple, simple,...) fields. This is the data for the table:
只是“ id”和“ name”(简单,简单,简单...)字段。 这是表的数据:
INSERT INTO `pagination` (`id`, `name`) VALUES(1, 'one'),(2, 'two'),(3, 'three'),(4, 'four'),(5, 'five'),(6, 'six'),(7, 'seven'),(8, 'eight'),(9, 'nine'),(10, 'ten'),(11, 'eleven'),(12, 'twelve'),(13, 'thirteen'),(14, 'fourteen'),(15, 'fifteen'),(16, 'sixteen'),(17, 'seventeen'),(18, 'eighteen'),(19, 'nineteen'),(20, 'twenty'),(21, 'twenty one');
Ready to go. Here's the index.php file
准备好出发。 这是index.php文件
"; // DISPLAY THE PAGES LINKS for($i=1;$i<=$pagecount;$i++){ echo"$i | "; } // THE CURRENT PAGE NUMBER FROM GET VARIABLE // IF NOT SET - IT'S FIRST $page = 0; if(empty($_GET[page]) || !isset($_GET[page])) $page = 1; else $page = $_GET[page]; // WHERE ARE WE ? echo"current page: $page"; // STARTRECORD IS USED FOR THE MYSQL QUERY // SO WE KNOW WHERE WE CAN START FROM // LIMIT WITH IPP (ITEMS PER PAGE) if(!empty($page)) $startrecord = ($page-1)*$ipp; else $startrecord = 0; $query = "select * from pagination limit $startrecord,$ipp"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo"$row[id], $row[name]"; } // THIS IS THE VALUE FOR THE NUMBER OF // NEXT AND PREVIOUS PAGES DISPLAYED IN // THE DYNAMIC PANEL $margin = 2; // THE DYNAMIC PANEL echo""; echo"Page "; if($page == 1); else { $prev = $page - 1; echo" first "; echo" prev "; } if($page-$margin <= 0) $start = 1; else $start = $page - $margin; if($page+$margin > $pagecount) $last = $pagecount; else $last = $page + $margin; for($i=$start;$i<=$last;$i++){ // SHOW THE CURRENT PAGE WITH DIFFERENT FORMATTING if($i == $page) echo" $i "; else echo" $i "; } if($page == $pagecount); else { $next = $page + 1; echo" next "; echo" last "; } echo""; ?>
This is all you need. The comments are there, but to make myself clear - I will add explanation below. I hope the top part of the script is obvious, we have to connect, display errors and know how much records in the table we have:
这就是您所需要的。 那里有评论,但为了使自己清楚-我将在下面添加解释。 我希望脚本的顶部是显而易见的,我们必须进行连接,显示错误并知道表中有多少记录:
error_reporting(E_ALL ^ E_NOTICE); $link = mysql_connect("localhost","you","yourpass") or die(mysql_error());mysql_select_db("test") or die(mysql_error()); $query = "select count(*) as recordcount from pagination";$result = mysql_query($query) or die(mysql_error());$row = mysql_fetch_array($result) or die(mysql_error()); $numrows = $row['recordcount'];
Below: $ipp variable is for the Items Per Page, you would change it here to display the number of records you want.
下图:$ ipp变量用于每页项目数,您可以在此处进行更改以显示所需的记录数。
$ipp = 3;
Now we can count the number of the pages: let's say you have 501 records in your query and you set the $ipp to 100. So there are 6 pages with one item on the last - this is why we use ceil.
现在我们可以计算页面数:假设您的查询中有501条记录,并且将$ ipp设置为100。所以有6个页面上的最后一个是项-这就是我们使用ceil的原因。
$pagecount = ceil($numrows / $ipp);echo"We have ".$pagecount." pages";
Display navigation having all the page links, pointing each link to index.php,
显示具有所有页面链接的导航,将每个链接指向index.php,
having $page variable in $_GET[]. If the page variable is not set, the page number is 1. Then we output the page number - where we are.
$ _GET []中有$ page变量。 如果未设置页面变量,则页面编号为1。然后,我们输出页面编号-我们所在的位置。
for($i=1;$i<=$pagecount;$i++){ echo"$i | ";}if(empty($_GET[page]) || !isset($_GET[page])) $page = 1;else $page = $_GET[page]; echo"current page: $page";
And the heart of the pagination. We use the "main" db query to output the records FROM certain index with a limit of $ipp (items per page). You put your query instead of "select * from pagination". Display the records.
和分页的心脏。 我们使用“主”数据库查询从某些索引输出记录,记录的上限为$ ipp(每页项)。 您输入查询,而不是“从分页中选择*”。 显示记录。
if(!empty($page)) $startrecord = ($page-1)*$ipp;else $startrecord = 0; $query = "select * from pagination limit $startrecord,$ipp";$result = mysql_query($query) or die(mysql_error());while($row = mysql_fetch_array($result)){ echo"$row[id], $row[name]";}
That's it. Or isn't it ? Oh, yes - the dynamic panel.
而已。 是不是? 哦,是的-动态面板。
That is not necessary, but when you have 50 pages - might be handy. It shows the "middle" pages with a $margin variable - for adjacent pages - you can adjust and links to first / previous, next / last page. I've set it to 2.
这不是必需的,但是当您有50页时-可能会很方便。 它显示带有$ margin变量的“中间”页面-对于相邻页面-您可以调整并链接到首页/上一页,下一页/最后一页。 我将其设置为2。
The dynamic panel is divided into sections, this is the "first / previous" section
动态面板分为多个部分,这是“第一个/上一个”部分
if($page == 1);else{ $prev = $page - 1; echo" first "; echo" prev ";}
We have to know where to start and where to end the pages display, so we count that
我们必须知道页面显示的开始位置和结束位置,所以我们算了一下
if($page-$margin <= 0) $start = 1; else $start = $page - $margin;if($page+$margin > $pagecount) $last = $pagecount; else $last = $page + $margin;
Below is the section where we display the pages (the numbers).
下面是显示页面(数字)的部分。
for($i=$start;$i<=$last;$i++){ // SHOW THE CURRENT PAGE WITH DIFFERENT FORMATTING if($i == $page) echo" $i "; else echo" $i ";}
And this is the section for the "next / last" links.
这是“下一个/最后一个”链接的部分。
if($page == $pagecount);else{ $next = $page + 1; echo" next "; echo" last ";}
Let's hope it would work for you. If you want to praise / curse this tutorial, feel free to post a comment.
希望它对您有用。 如果您想赞美/诅咒本教程,请随时发表评论。
翻译自:
php分页代码简单实现
转载地址:http://fjhzd.baihongyu.com/