Forum Moderators: coopster

Message Too Old, No Replies

Paging through Records

PHP and MySQL

         

Maynard

3:31 pm on Feb 17, 2004 (gmt 0)

10+ Year Member



Hi there,

I have a PHP page that prints the results of a large database to the web page. I only want to show 10 records per page with dynamically generated pages with links top the next and previous 10 records.

How do I do this in PHP and MySQL?

Thanks,
Maynard.

Timotheos

4:31 pm on Feb 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this thread would help you...
http://www.webmasterworld.com/forum88/2451.htm [webmasterworld.com]

vincevincevince

1:40 am on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



add to your SQL statement "LIMIT $_REQUEST[startrec],".($_REQUEST[startrec]+10)
at the top of the page put:

echo "<a href=\"$_SERVER[PHPSELF]?startrec=".abs($_REQUEST[startrec]-10)."\">Previous 10</a> - <a href=\"$_SERVER[PHPSELF]?startrec=".$_REQUEST[startrec]+10."\">Next 10</a>";

try it ;) it's dirty, but it's quick

Maynard

10:45 am on Feb 18, 2004 (gmt 0)

10+ Year Member



Thanks everyone. I used the following in the end. Don't know if it's clean PHP but it works!

$perPage = 5;

if(!$pageNum)
$pageNum = 1;

$sql = "SELECT * FROM table ORDER BY id DESC LIMIT " . (($pageNum - 1) * 5) . ", $perPage";

// query database
// output to screen

// Next and Back:

<a href="results.php?pageNum=<?php echo $pageNum - 1;?>">Back</a>
<a href="results.php?pageNum=<?php echo $pageNum + 1;?>">Next</a>

Thanks,
Maynard.