Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination is necessary?

Is pagination important? What happens if you have like 200 entries. Will it slow down the website for people using it?

Sorry if this sounds like an easy question, just wondering, new to the game.

Thanks

Update:

After reading answers. I think its important that I use them. So how can i do search pages using limit. Not looking for a fancy solution..

like image 271
AAA Avatar asked Jan 21 '23 05:01

AAA


1 Answers

It's more efficient, quick, and server-friendly to break the results up into multiple pages.

For one, most users aren't going to look beyond the first X results regardless of how many there are on one page, so there would be a lot of results unused that were retrieved, processed, and sent to the client which is wasted bandwidth and CPU time. In fact, by paginating you increase the chance that they will continue on beyond where they would have stopped if it were all on one page.

Two, if this were just text data it would be pretty quick even for a large amount of data, but if you throw in pictures and stuff that have to be displayed for each result, such as in a catalog, then things really start slowing down the more results which are returned.

Pagination via LIMIT in SQL is done by using a Limit range. When a single argument is given for LIMIT in an SQL query, then it will return up to that amount of results, so SELECT * FROM table LIMIT 5 will return at most 5 results. When two arguments are given, it skips results until it reaches the first argument, then returns up to the next number of results. So, SELECT * FROM table LIMIT 40,10 will return at most 10 records starting from the 41st result.

So, what you need is some way to tell the server what page or result the client is wanting, usually this is done by the pagination links with something like: http://www.domain.com/search.php?p=4 or http://www.domain.com/search.php?r=40, which you then use that value via $_GET to determine what the limits in the SQL query would be. Something like (using passing the page number via get in this case):

<?php
$sql = "SELECT * FROM `table`"; //Set up initial query string

$max_per_page = 20;  //Max results per page, can be drawn from elsewhere/configuration file or something or declared directly
if(isset($_GET['p']))  //Check to see if $_GET['p'] is set
{
     $current_limit = abs((int)$_GET['p'] - 1) * $max_per_page;  //Current limit = max * (current page - 1) other wise, page 2 would have a starting point of 40 instead of 20 like it should.  When passing the result number to start on, you'd just use that instead
}
else
{
     $current_limit = 0;
}
$sql .= ' LIMIT ' . $current_limit . ', ' . $max_per_page;  //Construct LIMIT on end of query string
$result = mysql_fetch_assoc(mysql_query($sql));
?>

Oh, and to figure out the total results, you can do a SELECT COUNT(*) FROM table WHERE etc. etc. type statement to start with so you know the total results, which can be used to determine how many pages are necessary since you know the max results per page.

like image 62
Phoenix Avatar answered Jan 27 '23 16:01

Phoenix