limit per page with Previous / Next Links

Codes here !

Moderators: macek, egami, gesf

Post Reply
User avatar
Fife Club
New php-forum User
New php-forum User
Posts: 15
Joined: Wed Oct 09, 2002 7:50 pm
Location: Charlotte, NC, USA

limit per page with Previous / Next Links

Post by Fife Club » Tue Oct 22, 2002 6:20 pm

Somebody else made this suggestion for limiting results to 15 per page with previous/next links:

Have a $page variable...


--------------------

if(!isset($page)) { $page = 0; }

$per_page = 15;
$limit = $page * $per_page;

$query = "SELECT * FROM table LIMIT $limit,$per_page";

--------------------


Something like that...For your next link, you pass $page++, for prev, you pass $page--. Need to throw in some extra stuff that checks the limits of the pages, though....

--------------------

echo "<a href='$PHP_SELF?page=" . $page-- . "'>Prev</a>";
echo "<a href='$PHP_SELF?page=" . $page++ . "'>Next</a>";

--------------------


I can't find this person anymore so I'll ask those of you who may understand.

I added this to my exisitng code and the first page works fine. All other pages work fine if you type "?page=3" or such in the url. The problem seems to come from the previous / next links generated at the bottom. The previous link always links to the CURRENT page and the next link always links to the PREVIOUS page.

See it here:
http://www.pstvalumni.com/directory/index3.php

Or here in a more simplified way with all ORDER BY complications removed:
http://www.pstvalumni.com/directory/index4.php

I don't understand how the last two lines were created so can anybody help me fix them, please? Or can somebody give me a better solution altogether?

Jay

Post by Jay » Wed Oct 23, 2002 2:01 am

The last 2 lines should be:
echo "<a href='$PHP_SELF?page=" . ($page-1) . "'>Prev</a>";
echo "<a href='$PHP_SELF?page=" . ($page+1) . "'>Next</a>";

Whoever wrote that script performed multiple functions on the same variable without taking into account that it would change it before the next!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest