-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.

2
  • Search for a tutorial on pagination Commented Oct 9, 2015 at 19:59
  • guys please don't mark as duplicated.i cant get what i wanted in other questions. but i got my answer here.good luck! Commented Oct 11, 2015 at 14:32

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

i thought i can only give one argument to LIMIT. thanks.but how can i make a button for that?
1

Try using the OFFSET query

SELECT * FROM `table` LIMIT 50 OFFSET <amount>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.