Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods to achieve first, previous, next and last navigation

I'm new to programming and have a question about navigation. What are the various methods that can achieve a "First", "Previous", "Next" and "Last" navigation, as seen in article or comic based sites? (Admittedly the "First" link isn't confusing as it can stay static, but what about the others?) For example, on the 50th page, the links would appropriately lead to the 49th and 51st (if it exists, if not it would not function but would automatically become active when such a page exists. In most examples of this I see urls ending with something like ".php?id=50" but am not certain how it's achieved, with a database? Any help will be very much appreciated, thanks.

like image 944
Koroviev Avatar asked Jan 22 '23 11:01

Koroviev


1 Answers

The bit at the end of the URL is called a GET variable. When a user goes to a page with one at the end, for example:

example.org/comics/myawesomecomic.php?page=50

The server goes to that page and (usually) a server-side script will do some work before outputting the page to the user. With PHP, the GET variable is a Global Variable (along with others, the big ones being POST and COOKIE), that the php script can retrieve by getting the value passed from the server to script, in this case in $_GET['page'].

So assuming you have a script that handles all requests for the comic storyline "My Awesome Comic" and it's broken up into 60 pages, all a user has to do to get to any page is change the GET variable to the page number. Your script might do something really simple, like get the contents of a directory called myawesomecomic/ and grab a file called 50.html and then output it to the user. But chances are, you want something a tad more sophisticated since you went to this trouble, so instead it goes and grabs page 50 from a database and updates all of the navigation and maybe even throws a stylish 50 - 60 in the top corner.

Now, here's the easy part, aka the answer to your question. Now that you know how GET variables work and what the server-script does with them, all you have to do to make navigation like you describe is:

$current_page = $_GET['page'];
$next_page = $current_page + 1;
$prev_page = $current_page - 1;
$first_page = 1;
$last_page = 

And that last part is going to be up to you to know already. Either you can always have the same number of pages, or you can query the DB at the same time you pull up the current page, etc. Let's say you did query the DB for the total page count while you were getting the current page. And it returned 60 (and we'll assume, just to avoid headaches that 60 is the page count starting from 1, not 0), so back to the navigation...

$last_page = 60;

Now you have your first, last, prev, and next. Assuming you know your way around HTML, all that's left (besides making this look pretty) is:

 echo "
 <a href='myawesomecomic.php?page=$first_page'>First Page</a>
 <a href='myawesomecomic.php?page=$prev_page'>Previous Page</a>
 <a href='myawesomecomic.php?page=$next_page'>Next Page</a>
 <a href='myawesomecomic.php?page=$last_page'>Last Page</a>
 ";

And there's your navigation. Obviously you'll want to arrange them to fit your site, not have them run together like that, use cool arrows, etc. But the links will work even if you don't do anything fancy.

BUT!

I just gave you the really nice, "my first time having fun with PHP" version. Things I left out (that you should ask more about or do some serious research on):

  • Since GET Variables are including on the URL, they aren't hard to figure out. I could easily change ?page=50 to ?page=1=1 and, depending on how you are getting the content, I may have just asked for all contents of your database. Never grab user-input (even the URL) straight into your variables. If you know it's page number, then you know it's a number. Check the input to make sure it's a number before running it through your script and your database.

  • You need to have a system for if the user puts in a number and it's out of range. Something simple like "Sorry, page not found" or even "Page not found, starting at the beginning" and sending them back to page 1, whatever. If you just output nothing, it looks bad and you reveal parts of your script's logic that could be manipulated.

  • When you get comfortable with GET variables, I suggest ditching them for mod_rewrite (assuming you are on an apache server). Don't jump in now, but when you get the hang of it, you can start making URLs like: example.org/comics/myawesomecomic/page50 or example.org/comics/myawesomecomic/page/50 or what have you. What really happens is all pages in that directory get redirected to the same script which then checks the URL's ending to figure out what to pull from the DB. But to the end-user, it looks like there's just a webpage. This looks cleaner (I think) and does even more to cover your script's logic. But I'm saying to wait on it because it also opens you up for other risks that can be harder to anticipate until you're used to sanitizing your user-input and handling standard page errors like "page not found."

Anyways, figuring out stuff like adding a week to a current date and adding a page number to the current page is totally how I got into PHP and web app development, so I hope this was helpful and straightforward.

like image 58
Anthony Avatar answered Jan 25 '23 02:01

Anthony