Skip Navigation LinksHome > Categories > Code from a Category

Splitting the data in PHP and MySQL

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1925
Add Date: 04/02/2012
In this article I will discuss how to implement it on your site using PHP and MySQL. In the above example will output reference "forward" and "backward", "on the first page" and "last page", and we can limit the number of links that are displayed simultaneously.


Now, many sites use to store the database in which the number of records is often in the hundreds and thousands. Therefore, for the convenience of users, portions of records are displayed, that is, page by page.

To begin, I will explain the principle of the pager, and then we are ready to look at an example.
Because on one page must be a certain number of records, then we limit it to a database query by using the LIMIT, and OFFSET parameter will indicate how to start writing a conclusion.
For example limit the number of records for two on each page, and start the output from the first page (ie, with zero entries).

SELECT * FROM postranichno LIMIT 2 OFFSET 0;

To print the second page we need to increase the value of OFFSET to the number of entries, that is two.

SELECT * FROM postranichno LIMIT 2 OFFSET 2;

Since the value of OFFSET is constantly changing, we assign a variable number of records $ quantity, and the number with which to start a conclusion, we will determine by multiplying the number of pages by the number of entries, and assign the variable $ list, and then substitute all of this in the request.

Ready example with comments

First of all, create the file, which will connect to the database MySQL. We'll connect it to all the scripts that need to query the database.

To be on the test than the pagination script, we create a table using the install.php in a MySQL database and add seven records.

Run the install.php to records added to the database. Now start writing the main PHP-script postranichno.php, which will be paged out to the database MySQL.
All the actions described in the comments for a better understanding of what we do.


How do you remove unwanted features

I identified two blocks of code. The first one implements the output links "back" and "on the first page." The second, respectively, the "forward" and "on the last page." If these features you do not need, then just delete the code blocks.
The sample also limited the number of links that are displayed before and after the current page, so that they do not fill the entire browser window. If you do not need it, then change the value of $ limit on the number that is greater than the number of pages, for example, 3000.

A few tips

To reference numbers correspond to the number of pages and counting did not start from scratch, and on the unit, I had a bit more complicated code.
I hope that the comments have helped you to understand.
You can change the number of records simultaneously displayed on the page you are on the right is an integer - in the script is not affected.
Similarly, you can simply change the file name postranichno.php to another without changing the code.

By posting on its website a link to the first page put it right on the postranichno.php? Page = 1, and not just postranichno.php. Both versions give the same result, but the first would be more correct choice.

If you remove all the comments, you will notice that there is not much code, and it's easier than it seems at first glance.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.