Re: Trying to paginate a code but having trouble.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux