This is one of the most asked question I've ever seen in dev communities! it's called pagination.
It's been asked too many times, so I thought this might be the first tip I should add here !
Simply, all you have to do is add the LIMIT clause to your MySQL query and it should work fine!
Sounds easy, doesn't it ? ;)
Now all let's see an example:
so the above query will fetch only the first 3 rows in the table.
There is another way to use LIMIT (which is the one we need!):
This code will allow you to fetch 10 records starting from record 0 (the first), this is helpful when you have hundreds of records in the table and you want to show only part of it or make every set of the result in a page.
Now how can you make the next page show the next 10 records?
you simply have to store the value of the starting row in a variable and pass it in the URL as a GET variable.
We also have to check if there was a value already passed or not so we can set a default value in case it wasn't (zero to start from first row):
Now your query should have this new variable ($startrow) in the LIMIT clause
Now to see the next 10 records you should have a link which will add 10 to $startrow so you can view the next 10 records
It's that simple !!
if you want to have a previous link, just substract 10 from $startrow like this:
It's been asked too many times, so I thought this might be the first tip I should add here !
Simply, all you have to do is add the LIMIT clause to your MySQL query and it should work fine!
Sounds easy, doesn't it ? ;)
Now all let's see an example:
PHP Code: |
<?PHP |
so the above query will fetch only the first 3 rows in the table.
There is another way to use LIMIT (which is the one we need!):
PHP Code: |
<?PHP |
This code will allow you to fetch 10 records starting from record 0 (the first), this is helpful when you have hundreds of records in the table and you want to show only part of it or make every set of the result in a page.
Now how can you make the next page show the next 10 records?
you simply have to store the value of the starting row in a variable and pass it in the URL as a GET variable.
We also have to check if there was a value already passed or not so we can set a default value in case it wasn't (zero to start from first row):
PHP Code: |
<?PHP |
Now your query should have this new variable ($startrow) in the LIMIT clause
PHP Code: |
<?PHP |
Now to see the next 10 records you should have a link which will add 10 to $startrow so you can view the next 10 records
PHP Code: |
<?PHP |
It's that simple !!
if you want to have a previous link, just substract 10 from $startrow like this:
PHP Code: |
<?PHP |
No comments:
Post a Comment