473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How could I paginate the result set?

I would like to do something like this:

select * from myTable where ... pagesize 10 page 1

How could I do it elegantly in:

1. PHP+MySQL before MySQL 5 (without stored procedures)
2. MySQL alone (with stored procedures)

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 19 '07 #1
9 1767
"Man-wai Chang" <to***********@gmail.comwrote in message
news:46********@127.0.0.1...
I would like to do something like this:

select * from myTable where ... pagesize 10 page 1

How could I do it elegantly in:

1. PHP+MySQL before MySQL 5 (without stored procedures)
2. MySQL alone (with stored procedures)

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Hi. You can use the query to do it for you, without PHP or stored
procedures.

SELECT * FROM table LIMIT 0, 10

Where 0 is the start record, and 10 your pagesize.

Regards, Hans
Apr 19 '07 #2
Man-wai Chang wrote:
I would like to do something like this:

select * from myTable where ... pagesize 10 page 1

How could I do it elegantly in:

1. PHP+MySQL before MySQL 5 (without stored procedures)
2. MySQL alone (with stored procedures)
Take a look at LIMIT in MySQL, and use a page offset variable in your PHP.

I usually do two queries, one to get how many results there would be
without pagination, and another to get the results within the page range.

Don't know if this is the best way to do it though, i'd be interested in
others replies myself.
Apr 19 '07 #3
I usually do two queries, one to get how many results there would be
without pagination, and another to get the results within the page range.

Don't know if this is the best way to do it though, i'd be interested in
others replies myself.
Mind to publish your codes? :)

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 19 '07 #4
Hi. You can use the query to do it for you, without PHP or stored
procedures.
SELECT * FROM table LIMIT 0, 10
page 1 is easy. What about pageno 1?

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 19 '07 #5
Man-wai Chang wrote:
>I usually do two queries, one to get how many results there would be
without pagination, and another to get the results within the page range.

Don't know if this is the best way to do it though, i'd be interested
in others replies myself.

Mind to publish your codes? :)
define('PAGE_SIZE', 5);

$cat_id = @(int)$_REQUEST["cat_id"];
$page = @(int)$_REQUEST["page"];

/*
* find out how many recipes in total for paging, if request
* page>total pages then reset page variable to 0
*
*/
$sql_pagesize = "SELECT COUNT(cr.catID) AS count FROM catrecipe cr,
recipe WHERE cr.catID = recipe.catID AND cr.catID = " . (int)$cat_id;
$ds = mysql_query($sql_pagesize) or trigger_error("Unable to query
database: " . mysql_error());
$dr = mysql_fetch_assoc($ds);
$article_count = $dr["count"];
$pages_count = ceil($article_count/PAGE_SIZE);
if ( $page $pages_count ) {
$page = 0;
}
@mysql_free_result($ds);
unset($dr);
unset($ds);

/*
* work out the start and end for the SQL LIMIT in the paging option
*/
$start = 0+($page*PAGE_SIZE);
$end = $start+PAGE_SIZE;

/*
* look up the categories for the current page being viewed and display
them
*
*/

$sql = "SELECT cr.catID, cat_title, recipeID, recipe_title
FROM catrecipe cr, recipe
WHERE cr.catID = recipe.catID AND cr.catID = " . (int)$cat_id
.. " ORDER BY recipeID DESC LIMIT {$start}, {$end}";
$ds = mysql_query($sql) or trigger_error("Unable to query database: "
.. mysql_error());
Apr 19 '07 #6
Tyno Gendo wrote:
<snip>
>
$sql = "SELECT cr.catID, cat_title, recipeID, recipe_title
FROM catrecipe cr, recipe
WHERE cr.catID = recipe.catID AND cr.catID = " . (int)$cat_id .
" ORDER BY recipeID DESC LIMIT {$start}, {$end}";
$ds = mysql_query($sql) or trigger_error("Unable to query database: "
. mysql_error());
note that my $page var is 0 based. so if you display page to the user,
use $page+1 so it doesn't say 'You are viewing Page 0' :)
Apr 19 '07 #7
In article <46********@127.0.0.1>, to***********@gmail.com (Man-wai Chang)
wrote:
>
select * from myTable where ... pagesize 10 page 1

How could I do it elegantly in:

1. PHP+MySQL before MySQL 5 (without stored procedures)
2. MySQL alone (with stored procedures)
Look at SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()

These tell you how many rows would have been retrieved without a LIMIT
So when you do a query for (RECORDS_PER_PAGE) records, you know what the
total would have been and can set your navigation etc.

For a page part-way through, you take the record or page number that is
input to tell you which page, to tell you which records to retrieve.

So SQL_CALC_FOUND_ROWS and something like this goes in your query:

$this->pageno = 1;
// Sanitise
if (isset($_GET['page'])) $this->pageno=max((int)$_GET['page'],1);
// Offset is 0
$firstrecord = ($this->pageno - 1) * self::RECORDS_PER_PAGE;
$query .= " LIMIT $firstrecord, ". self::RECORDS_PER_PAGE;

And your navigation links are something like this: (watch out for the
wrapped lines) (SqlRun is a utility subroutine that runs and error traps a
query)

$max = SqlRun("SELECT FOUND_ROWS()");
$total_records = mysql_result($max, 0);
mysql_free_result ($max);
if ($total_records self::RECORDS_PER_PAGE) { // Pagination required
$this->count = $total_records;
$pages = (int)ceil($total_records / self::RECORDS_PER_PAGE);
$prev = $this->pageno - 1;
$next = $this->pageno + 1;
$work = 1==$this->pageno ? '|&lt;- First &lt;-Prev' : "<a
href=\"{$_SERVER['SCRIPT_NAME']}?page=1\">|&lt;- First</a<a
href=\"{$_SERVER['SCRIPT_NAME']}?page=$prev\">&lt;-Prev</a>";
$work .= "&nbsp;&nbsp;&nbsp;<strong>Page {$this->pageno} of
$pages</strong>&nbsp;&nbsp;&nbsp;";
$work .= $pages==$this->pageno ? 'Next-&gt; Last-&gt;|' : "<a
href=\"{$_SERVER['SCRIPT_NAME']}?page=$next\">Next-&gt;</a<a
href=\"{$_SERVER['SCRIPT_NAME']}?page=$pages\">Last-&gt;|</a>";
$this->pagination = "<p class=\"pagination\">$work</p>\n";
}

Then $this->pagination can be output as required.

--
To reply email rafe, at the address cix co uk
Apr 19 '07 #8
Man-wai Chang wrote:
Hi. You can use the query to do it for you, without PHP or stored
procedures.
SELECT * FROM table LIMIT 0, 10

page 1 is easy. What about pageno 1?
Using PHP (with explanations):

// Number of items per page.
$pagesize = 10;

// Get the page number from the user input.
$pageno = intval($_GET["page"]);

// In case "page=" in the URL query string was set to 0,
// a negative number, or not set at all.
if ($pageno < 1) { $pageno = 1; }

// Calculate the offset based on the page size and given page number.
$offset = ($pageno - 1) * $pagesize;

// Perform the
$query = "SELECT * FROM myTable LIMIT ".$offset.", ".$pagesize;
$result = mysql_query($query);

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Apr 19 '07 #9
thank you all.

--
iTech Consulting Services Limited
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Apr 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jerrygarciuh | last post by:
Hi all, I am iterating through a result set to generate a second set of queries but no matter what I do I get the error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result...
2
by: maceo | last post by:
I have a script that will print out the results of a table and make a calculation of a total of one of the columns. See example: <?php /* Database connection */...
4
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am quite confused on a equation, as following: #include <iostream> int main(){ int i = 2;
1
by: VB Programmer | last post by:
Is it possible to paginate or sort a datalist in ASP.NET 2.0 easily (as it is with the amazing datagrid)? Thanks!
2
by: vacuno | last post by:
Hi! Im reading a xml file via http and writing this content in browser with this code in vbscript/asp: ************* <% dim xmlDom, oNode, nodeCol set xmlDom =...
8
siridyal
by: siridyal | last post by:
I have a wholesale website that i'm working on that shows hundreds of items that are updated from time to time. These items are kept in a mysql database with several tables. I want to let the...
1
by: asevilla | last post by:
Hi, i have the following XML <devices> <device id="XXX"> <name> .... </name> </device> </devices> There are like 1000 devices, I need the Xpath...
4
osward
by: osward | last post by:
I had made a table colum sortable and paging the table, following are the code // Display Event List echo "<center>"._EVENTLIST."</center><br>"; $now = Date(Y-m-d); // sort table...
10
Fary4u
by: Fary4u | last post by:
I've recently find some Paginate Plugin but it's only came with NEXT & PRV HERE IS THE CODE eval(function(p,a,c,k,e,r){e=function(c) {...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.