pagination with php with a possible infinite limit - php

I know how to create pagination with php if I have 25 results with 5 results each page, but what about if the limit number can change in the hundreds everyday? How can I write something where the php itself will create more pages if the row count itself changes?

usually pagination is made in two steps:
query to count the number of records
unsig the prevoius query, create the query using LIMIT to retrieve the current page
So i don't see the problem o_O, if tomorrow the number of elements change then the first query would return a different result.. and you wont have problems with the pagination
Later, if you want to improve performance you could set a cache system for the count query.
Hope this helps

Related

Mysqli php ajax Pagination

can someone please explain to me how to approach this issue?
I have a table with 4000 records, a select option and a search field to filter out the table
however I'm trying to figure out the best way to set up pagination.
1st Approach:
Should I make one query against the database and populate the table with all records and then set up the pagination with javascript so I can be able to search for records that are not only shown on the current page?
2nd Approach
Set up the pagination with PHP, make a query via ajax for each page? however I'm afraid that this approach would not allow me to filter out the table if I need to search for a record that is not on the current page.
1st approach: definitely not, loading large number of results for nothing is not recommended.
2nd approach makes more sense. You can use the LIMIT statement inside your SELECT statement to decide which records you want. Ex. if you paginate on 10 elements perpage, you could do
SELECT * FROM Table LIMIT 10
then
SELECT * FROM Table LIMIT 10,10
and so on. Indexes start at 0.
Note that you don't even need Ajax to do that, your next button can specify the offset for the next load of the page. It's a choice based on your knowledge, the size of the rest of the page (minimize load time), ...

How to write pager effect in PHP

I'm sorry, I am a newbie in PHP. Now I want to learn how to write a pagination effect in PHP. I know there are many tutorials for this on the net. I searched it and have a reference to them. When I try it by myself and don't have a reference to other resources, though, I still don't know how to write a pagination effect with PHP.
Now, I want to know what's the basic and important thing of writing a pagination in PHP. If anyone could give me more details, thank you.
For this example, I will use MySQL because it's a popular database to use with PHP. The general principle applies to other databases, but the precise way you use SQL to achieve the results is different.
The essence of pagination is that you have a number records, say 105, and you only want to display a smaller, more manageable number of records at a time, say 10 of them. In order to do this, you need to know how to find out the total number of records, and you need to know how to select just a subset of the records.
First, find out how many records you have. For this, you use COUNT(*) to count the records in the table. If your table is called Users:
SELECT COUNT(*) FROM Users;
Since we said there are 105 records in the table, this will return the result 105. We said each page has 10 records, so figure out how many pages there are in total: 105 / 10 = 10.5; round up to 11 because the extra records after page 10 need their own page. So, there are 11 pages in total. Your web page will display controls that enable the user to select a page from 1 to 11.
Instead of fetching all the records, you will only fetch one page at a time. In MySQL you generally do this using the LIMIT keyword; this allows you to choose a range of records. LIMIT syntax works like this: LIMIT $SKIP, $COUNT, where $SKIP is the number of records in the result set to skip, and $COUNT is the number to return. (Actually, you will get up to $COUNT records, in case fewer are available.)
The user has requested page 6. This means that I need to skip 5 pages before the results I want to show. With pages of 10 records each, this means I will skip 5 * 10 records, i.e. 50 records. In other words, $SKIP = ($PAGE_NUMBER - 1) * $PAGE_SIZE. You query would look like this:
SELECT User_ID, UserName, City, State from Users LIMIT 50, 10;
This gives you records 51-60 of the table.
See the documentation to learn more about how LIMIT works:
http://dev.mysql.com/doc/refman/5.5/en/select.html
Now you have a single page of records. However, the numbering of the records is dependent on their ordering. The query above will give you unpredictable results because the ordering is undefined. (A database can return records in any order it chooses unless you command it to use a certain ordering.) Thus, to do pagination, you need to define the order of the records. Use ORDER BY to define the ordering:
SELECT User_ID, UserName, City, State from Users ORDER BY UserName LIMIT 50, 10;
This allows you to show different pages of results, and always know that when you show page 6, it contains the records that come after the ones on page 5 and before the ones on page 7.
(If your records don't have any natural ordering, you will have to define an arbitrary one to make this work correctly.)
I have avoided mentioning PHP at all in this answer because I don't think it's necessary to do so to explain pagination. Pagination is entirely a matter of handling data. If you understand this, I think you can go read the tutorials and they will make sense to you and then you can figure how to write the PHP to do this.
Get page number
Know how many items to display on each page.
Using mysql command "LIMIT", get the offset and select how many rows you want.
Want to use a class? http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/
Or just a plain script to modify? http://pastebin.com/J8nTk1q5
If you are new to PHP, then you need to learn more PHP or risk future confusion.

PHP MySQL pagination results

ive been fiddling with pagination and managed to get a version working based on a script, the code from the php cook book did not work.
The pagination i example i have working is from this site:
http://www.developphp.com/view_lesson.php?v=289
I notice there are 2 queries, firstly to get total numbers of rows then to show rows, am not sure if i should be having 2 queries, another example on this site also shows 2 queries: http://www.phpeasystep.com/phptu/29.html
Contrasting information on the net and in books, should i be using 1 or 2 queries for pagination results? I do not want to continue to pick up others bad habbits so thought i would ask the pro's
Thanks
I don't know of any way to get the total number of results and the current page of results using one query, but it's possible to use an easier method.
SELECT SQL_CALC_FOUND_ROWS *
FROM ...;
And then
SELECT FOUND_ROWS();
Which returns the number of rows that were found (but not returned if there was a LIMIT) in the previous query.

Display sql results in groups

I have an sql query here and it returns a number of results. I'd like to show these results in groups.
What I mean is,
show the first 20 results in some part of the page,
show the next 20 results in another part of the page
etc...
How can I do that?
*I'm using PHP to display results.
What you want is called pagination and the specific implementation depends on the database. For example, in MySQL you can use LIMIT a,b, and most other databases you can use either TOP(n) or ROW_NUMBER.
do your SELECT command with LIMIT statement.
on first query you can get first 20 results, then next 20, etc.
Ok I found a solution in a forum. It's here in case somebody else needs it
http://www.phpbuilder.com/board/showthread.php?t=10311631

What would i need to do display urls in order the way they are displayed on SO?

I'm really unsure of how ordered results are displayed on a page like stack overflow questions. I know how to get the data from the DB but do i use a for each loop to display the data. And how can i paginate?
I know all about display logic on the databse end. What i mean is that i have the data ready but i do not know how to put in on the page to be displayed correctly.
Thanx
You need to use
ORDER BY for sorting your results
LIMIT for pagination
GET parameters in your requests should determine the values applied to those statements.
To order results, you should add a "ORDER BY" clause to your SQL statement. Specifically, for StackOverflow default view, it shows questions by date (with the most recent most). This implies that each question has the datetime which it is created tagged to it.
As for pagination, here's a general outline of the code
Find total number of records
Decide how many records per page
Find out how many pages (record per page/page)
Check what's the current page number
Calculate the parameters to add to the LIMIT clause in the SQL statement. The start record should be current page * records per page.

Categories