Re: Trying to paginate a code but having trouble.

From: Date: Thu, 14 Feb 2008 22:41:31 +0000
Subject: Re: Trying to paginate a code but having trouble.
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-44604@lists.php.net to get a copy of this message
Chris Carter wrote:
Hi, I am trying to paginate this to display just 4 results and use <Previous and Next> options for pagination. Please help. <div class="content"><p><?php // database information include "includes/config.php"; $query="SELECT * FROM students LIMIT"; $result=mysql_query($query);
You need to use LIMIT offset, number. eg: limit 50, 10 <- start at position 50, get 10 records. http://dev.mysql.com/doc/refman/5.0/en/select.html To make this work reliably, you also need to order your results. Unless you specify an 'ORDER BY' clause the database will return them in any order it sees fit and probably won't order things correctly. It might get it right a lot of the time but the only way to guarantee it is to tell it how to order the results. So you should end up with something like this: <?php $number_to_fetch = 10; $start_page = (isset($_GET['Start'])) ? (int)$_GET['Start'] : 0; $offset = $start * $number_to_fetch; $query = "select * from students ORDER BY student_id LIMIT " . $offset . ", " . $number_to_fetch; Then when you go to the next page, 'Start' will be 1. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.db (#44604) next »