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

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

Related

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

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>

Multiple pages for results

On my search engine I get results via xml from blekko.com
I use: http://blekko.com/ws/?q=google/rss
And adding: &p= *number*
I can get different pages of results, e.g.
Page 1, 1 - 15 results
Page 2, 15 - 30 results
Like at the bottom of google, however I also get a number of total results
With those two (total & page number) is there any way to generate the right amount of links at the bottom for a next button, page1 results, page 2 results ect...
$count is the total and there are 15 results per page
If I understand you correctly you want something like:
for($i; $i<10 && $i<$totalPages; $i++)
{
echo ''.$i.'';
}
?
Not sure of what you want to do but usually to create links to different pages of results you need the total number of results and the nb of results you want to display per page. You then make a simple division to get the number of pages.

Calculate number of pages using PHP Maths and Round

I have a given number of potential posts. We don't know how many there are but the system is set up to show 12 per page. Along the bottom I would like it to display the number of pages.
So first if we get the posts:
<?php $pages = get_posts('category_name=news'); ?>
Now what we want to do is
work out how many posts it has found
divide that number by 12
round that number up to the nearest whole number (UP never down)
divide that number by 1 and give how many times 1 goes into it.
thus giving as many page numbers as needed.
The ideas is to have them lined up as 1 | 2 | 3 | 4 | 5 etc..
Any ideas?
You're over thinking it. If you know the number of results and the max number of results per page, then you know how many pages you need. I suppose this is WordPress because you've used get_posts, so that should return an array containing the posts, so:
<?php
$max_per_page = 12; //Max results per page
$posts = get_posts('category_name=news');
$total_posts = count($posts); //Total number of posts returned
$pages = ceil($total_posts / $max_per_page);
for($i = 1;$i <= $pages;$i++)
{
echo '' . $i . ''; //Or whatever the link needs to be
if($i != $pages)
{
echo "|"
}
}
?>
work out how many posts it has found
SELECT COUNT(*) FROM *table* WHERE *conditions*...
divide that number by 12
SELECT COUNT(*)/12 AS num_pages FROM *table* WHERE *conditions*...
OR
$count = mysql_query(*see #1*)/12.0; // NOT JUST 12!
round that number up to the nearest whole number (UP never down)
$count = ceil($count);
divide that number by 1 and give how many times 1 goes into it.
REALLY?? DIVIDING ANY NUMBER BY 1 RETURNS ITSELF!
thus giving as many page numbers as needed.
Not really. How would you know what particular page the user is currently on? How do you plan on actually paginating posts? If the posts are already populated, you are wasting 1-2 queries every time, just for your pagination.
You are basically trying to make pagination, but without knowing a lot of SQL, you're better off using an existing solution (or at least re-factor the existing code to limit queries)

select inbetween elements in mysql?

I am trying to implement the pagination in php. I am using the Mysql as back end database. I am trying to implement the pagination logic.
I would be having lots of record. But the user will see only 10 at a time.
Now to show the first page, i do a
SELECT * from USERS LIMIT 10.
Now to get the next 10 and the subsequent 10 records i am not able to write a query. Please help me fetch the in between records to support pagination logic. Also provide if any other suggestions for pagination.
You should use the OFFSET option.
SELECT * FROM Users LIMIT 10 OFFSET 10 (or 20, or 30);
That way you just pass the start position in the request when you hit next (or the page number) and you'll retrieve the records you want.
MySQL's limit feature can take two arguments:
select * from USERS limit 10,10
The above would retrieve 10 rows starting at row 10. Bear in mind that the MySQL row offset is 0 based, not 1. The first argument is the starting row, the second is the page size.
Also, if your page size is consistent, all you need to do is pass in the current page (default to zero). That would then allow you to specify the start row as a page * size.

How to perform page control?

I mean what the most efficient way to get information about the quantity of your page's items and make sql query with LIMIT that you need. or I should get all items and then crop array with php functions?
now I do 2 queries: first to count all items and second to get items that I need with LIMIT.
OK, I'll be more concrete. For example I need to show a question on my page and 20 answers to this question. At the bottom there shold be page control: links to the next, prev page and so on. I want to show proper number of links (number of answers/20) and when I go to any link I want to recieve proper answers (for example 41 to 60 on the 3d page). So what's the best way to get number of items (answers) to show proper number of links and to get proper answers for each link?
I guess your'e trying to say you want to know how many items/answers there is in the query but only read up to 20 items at at time, for pagination.
Firstly: You really should look for a pagination package; lots and lots of people have had the same problem before and there probably exists both free/opensource and proprietary solutions for your programming language and framework. (If you say what language you are using I'm sure someone can reccomend a solution for you.)
Anyway, I know I like to know how things work, so this is how it usually does:
As far as I know the pagination code calculates the pages by doing one query using select count(*) from tblX where something divide this number with the items-per-page number and use ceiling (e.g. 4.1 => 5).
For listing the results per page a new query is required; don't worry the count query is terribly much faster than getting every result discarding the ones you don't need DO NOT DO THAT (that's the recipie for becoming the top story on this page). Something like select * from tblX where something limit Y offset Z where Y is the number of items per page, and Z is the the (requested_page - 1)*Y; page 1 will have offset 0, page 2 have offset 20 (if thats what Y are) etc..
But do not try to implement this manually, it's unneccesary, tedious and error prone, much better to use your time customizing a readymade solution.
I'm assuming you want a count of the number of rows you'll be reading so as to do some pagination or similar? I don't understand your need for the LIMIT in the context of your question. However, if you just want a count of how many rows have been found, use one of the following.
You select the count of all rows such as:
select count(*) as counted, name, address
from contact
Or found rows:
SELECT SQL_CALC_FOUND_ROWS, name, address
from contact
This may be mysql specific I'm not sure.
Update:
For pagination you would do something like the following - (Psuedocode)
$rows = array($result)
$num_rows = sql_calc_found_rows
$per_page = 20
$pages = ceil($num_rows / $per_page)
page
$rows_this_page = array()
$rows_this_page = get_values($rows, (min index)$page_number * $per_page - $per_page, (max index)$page_number * $per_page - 1)

Categories