Ali SH Ali SH - 4 years ago 82
PHP Question

php - Making a pagination by Limiting results from Mysql Database

im making a script that gets some rows from mysql and returns them to user
after some time.those rows numbers got increased like 300 and now loading page takes a little time.
i wanted to page them.every page contain 50 of them so i have 6 pages and i mean:

row 1-50 in page 1
row 51-100 in page 2
row 101 to 150 page 3
row 151 to 200 page 4
row 201 to 250 page 5
row 250 to 300 page 6

i have some idea about limiting them by using LIMIT in my mysql query but dont know how to make button for it(page buttons)
i want the code to do this
sorry for my bad english, i hope you understand.

Answer Source

Use LIMIT in your SQL statements.

SELECT * FROM `wherever` ORDER BY `whatever` LIMIT 0,50

Then replace your starting point (0) with a PHP variable, and set that variable as $start = $page_number * 50;

SELECT * FROM `wherever` ORDER BY `whatever` LIMIT $start,50  

Read mysql LIMIT syntax here: The first number is the start, second number is how many more to go. Unless only one number is given in which case its how many more to go.

As far as a button is concerned, there are a few ways to do this. But the basic maths are here:

$total_rows = mysql_num_rows(mysql_query($original_query_without_limit));
$total_pages = $total_rows / 50;

$prev_page = $current_page - 1;
$next_page = $current_page + 1;

if ($prev_page > 0){
  // Print previous page link/button

if ($next_page < $total_pages){
  // Print next page link/button
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download