-2

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.

Ali SH
  • 403
  • 1
  • 6
  • 16

2 Answers2

1

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: https://dev.mysql.com/doc/refman/5.0/en/select.html 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
}
Andrew Coder
  • 1,058
  • 7
  • 13
  • i thought i can only give one argument to LIMIT. thanks.but how can i make a button for that? – Ali SH Oct 09 '15 at 20:08
1

Try using the OFFSET query

SELECT * FROM `table` LIMIT 50 OFFSET <amount>
Elipzer
  • 901
  • 6
  • 22