Trying to paginate a code but having trouble.

Trying to paginate a code but having trouble.

am 14.02.2008 19:38:19 von Chris Carter

Hi,

I am trying to paginate this to display just 4 results and use Next> options for pagination.

Please help.

// database information
include "includes/config.php";

$query="SELECT * FROM students LIMIT";

$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {

$a=mysql_result($result,$i,"a");
$b=mysql_result($result,$i,"b");
$c=mysql_result($result,$i,"c");
$d=mysql_result($result,$i,"d");


print'



'.$a.' '.$b.'

Name: '.$c.'

Price: '.$d.'



';
$i++;

}

?>

Thanks in advance,

Chris
--
View this message in context: http://www.nabble.com/Trying-to-paginate-a-code-but-having-t rouble.-tp15485452p15485452.html
Sent from the Php - Database mailing list archive at Nabble.com.

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

Re: Trying to paginate a code but having trouble.

am 14.02.2008 23:41:31 von dmagick

Chris Carter wrote:
> Hi,
>
> I am trying to paginate this to display just 4 results and use > Next> options for pagination.
>
> Please help.
>
>

> // 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:


$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