php - Making a pagination by Limiting results from Mysql Database [duplicate] - php

This question already has answers here:
PHP & MySQL Pagination
(4 answers)
Closed 7 years ago.
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.

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
}

Try using the OFFSET query
SELECT * FROM `table` LIMIT 50 OFFSET <amount>

Related

Selecting the next set of records from a MYSQL database table - PHP

I would like to know the syntax of how to construct a sql query that retrieves sets of data from the database. For example, I am able to retrieve a set of 10 records from a table , but then would like to retrieve the next ten after that based on the same query.
PHP
$limit = 10;
$SQl = "SELECT * FROM myTable LIMIT {$limit}";
What would the sql query be for the next 10 records, given that I already have the previous 10 records and would not want them to also be retrieved?
There are two options:
LIMIT 11, 10
and one more:
LIMIT 10 OFFSET 11
Legend:
11 - start row
10 - how much rows

Print mysql result 10 by 10 php css page break after

For example I have 100 result in my query.
I want to printing paper every page 10 result.
How to print php mysql query result 10 by 10 css page-break-after?
You did not ask your question very well in English.
But I think that you can run your query like this :
SELECT * FROM tableName ORDER BY fieldName DESC LIMIT $startIndex, $count
Which $startIndex could be your starting number and $count could be 10 in your case.
NOTE Do not forget to edit my code and use prepared statement in your query

display first n rows with pagination in mysql [duplicate]

This question already has answers here:
Simple PHP Pagination script [closed]
(3 answers)
Closed 6 years ago.
A mysql table contain 100 rows. I am trying to display first 20 rows with 5 rows per page.
I think query should be like this
SELECT * FROM `table` top 20 LIMIT 0, 5
but how can i use this concept. waiting for your help.
You can use LIMIT and OFFSET controls, LIMIT is for max rows you want to show and OFFSET is the starting position:
SELECT * FROM db_name ORDER BY db_table LIMIT 5 OFFSET 0

Displaying results hyperlink numbers like a search engine

Does anyone know of a good resource on how to create the hyperlink numbers at the bottom of a results page as search engines do to load the next number of results?
The page would load the first 10 results. And then if you click on the number, it loads corresponding results in that 10 number range.
Example:
0-10 -> show no numbers
11-20 -> show 1, 2
21-30 -> 1, 2, 3
up to 50
anything more than 50 does 1,2,3,4,5.....67 [last number].
My thoughts so far (I'm doing this in PHP/mysqli but the logic is more important than the code):
$total = mysqli_num_rows($result) //total number of reqults from sql query
if ($total>10) {
$last = intval($total/10) + 1 //get the last number of the results
if($last <= 5){
for ($i, $i<$last, $i++){
//print the numbers as hyperlinks
} else {
//print 1 through 5 ... then $last
}
}
This though is static from only 1-5...last number while the search engines have it so if you click on the number, it remembers that number and bases the new logic on it. So if I click on the 5 in my formula, it should change to something like:
[previous] 3,4,5,6,7....67 [next]
And then I would just pass the number to the page itself again and limit the results based on what number was passed. Any suggestions also on the best way to pass the info?
You are looking for a pagination script. Visit this link The page is in Arabic but forth post is of pagination and you can download source for english or arabic version of pagination
Basically, you need two values to create a pagination, a limit and a offset.
1.The limit is the amount of items your are displaying at the same time.
2.The offset is from where you started your query.
So, let's say you have 5 items in each page and 25 items total.
In your query, you have to limit 5,0 (the amount of items and the position the query will start).
Now, if you divide 5(limit)/25(total) and you'll get 5 (amount of pages).
Now in page 0 (the start) you can get the offset by multiplying the page number with the limit, so 0 (page) * 5 (limit) gives you 0 (in the first page you start from the offset 0).
Now in the 3rd page, you multiply 3(page) * 5 (limit) it gives you 15, which means in page 3 (or four if you take into account that you actually started at page 0) you will display from offset 16 to 20.
Finally in page 4 (which to your users will be page 5 because they started at page 1, not page 0) you will display from offset 21 to 25 which are all the items in your query.

How to do greater (>) and lesser (<) scripts?

I am using this script below. I need the "next" to show only when there are more than 10 entries.
code:
$fetch = mysql_query("SELECT * FROM table LIMIT $startrow, 10")or
die(mysql_error());
/*
this next options shows regarldess if there are more than 10 queries or less.
How can i make it so that it shows only when there are more than 10 queries.
*/
echo 'Next';
$prev = $startrow - 10;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
echo 'Previous';
You need to perform a separate query to count the number of rows in the table: SELECT COUNT(*) FROM table, and check if the returned number is greater than 10 $startrow + 10.
A word of advice. Don't rewrite something that's been done a thousand times. What you are trying to accomplish is called Pagination (Page by page displaying of data). It's commonly part of most PHP frameworks; such as CodeIgniter or CakePHP. There are also plugin classes out there which will help you greatly.
Google Search:
http://www.google.com/search?q=php+pagination
As other have already mentioned you'll typically need two queries. One query for the subset of results (number per page & an offset point) and one query for the total number of results. Try to find an existing class to help you with your paginating.
Good luck, Jeff Walters

Categories