Results per PageWe can allow the user to determine how many results per page he or she would like to see. To do this, we will provide a few links that will directly alter the value of $limit, this works because limit is in our query string ($limit = $_GET['limit']).
Here, we display the links to view different results per page:
See where we just alter the value of limit in the query string? This is also why we set the conditions above where $limit cannot be less than 10, or greater than 50.
Now we have all of our variables declared, and the query ran, so we are now at position to display our results. We will use a while loop to display the results, see the code below:
The code above will display each row matching the query we previously set. In the echo statement, you see result->title -This would display the title of the item in your table if you had a column named title. This is only an example, im sure the code you have to display each individual item is much larger and prettier, I just made it simple for you to see whats going on within the loop.
Displaying Prev., Next and pages in betweenSo, we've got our results matching the query displayed, now what about the rest? Believe it or not, the hard part is done and it should be smooth sailing from here. First, we will determine if we have to display the "Previous" link or not. Heres the code:
$prev_page: This contains the value of what will be our previous page, by taking the value of our current page, and subtracting 1. Next, we check if $prev_page is greater or equal to 1. If it is, we need to display the link -so we echo it. Take a look at the link we used:
Notice in the url that we simply include the variables containing our current data. I would also like to take the time to say that this is a perfect case to use Mod_Rewrite for friendly URL's. Instead of having this long tedious query string, you can make the URL appear as simple as this:
Where php is your $cat, 10 is $limit, and 1 is $page. Much, much better. And to possibly save you time, if you were to use this same exact format of url, your Mod_Rewrite code would be:
No Comments for this page.