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&