limit per page with Previous / Next Links



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:

Or here in a more simplified way with all ORDER BY complications removed:

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?
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!