Log in

View Full Version : Previous/next page links using PHP



Seaweedgirl
10-12-2011, 04:43 AM
Hi guys,

I was wondering if someone awesome would be able to help me out?

I have 80 php pages - I want to have next/back buttons on each page, automatically linking to previous/next pages ( page1.php, page2.php, page3, etc).

I would like an easier approach than having to manually link every button on each student profile page to go to the next/previous student pages.

*I really don't have enough time to set up a database or cms for this school project.


I would be very grateful for any help! :)

djr33
10-12-2011, 05:42 AM
There are plenty of examples of pagination in php if you do a quick search.

But most do involve using a database or at least some way of automatically generating the pages. And if you are using those as separate files, then honestly I don't see how automating the links will help-- you'll still need to modify each of those pages individually.

What I'd suggest would be to create a number of sub-pages, with your content. Then make one main page that will include the needed page.

So essentially:
1. At the top of your page, check for a variable in the URL, such as "?page=3".* Convert that to a variable like $page. (Do this using $page = $_GET['page'], plus error checking.)
2. Then take that number and look for your content page, such as "content/3.php".
3. Use include() to load the content into the current page.
4. You can keep standard layout (for example, shared <head> sections and navigation menus) in the main page, and you will want to have a shared footer for the links.
5. For each link, generate a link to the previous ($page-1) and next ($page+1) page. Use if statements to check to see if it is the first or last page, and if so skip the link (usually a grayed out [unlinked] "prev" or "next" is fine).

That's it.


Of course if you really want to do this using separate pages (although it WILL be more work), you can do the following:
1. On all 80 pages, include the following line where you want the prev/next links to appear:
<?php include('prevnext.php'); ?>
2. Create a file called prevnext.php (or whatever name you want).
3. In that file, determine the page number based on the URL. You could use regex (faster) or string functions (more work)-- split after the last slash, split before the dot, and then split after "page". And add error checking. Save this value as $page.
4. Now output your links as desired, subtracting/adding 1 from the current $page value.



*Note: if you absolutely hate having complicated looking URLs (for example, look at the URL for the page you're currently on to see this discussion), then you may want to look into "pretty URLs", which generally describes a few techniques to make the URLs look pretty for the end user, but gives you the same control on the server. So if your whole reason for using different pages is that, it will save you a bit of time in the end if you work out a way to automate it. (Basically you'll be lying to the visitor, and using one file to display what the server claims are many pages-- but it's much easier for you, and for the user won't be any different.)

Seaweedgirl
10-12-2011, 08:51 AM
Thank you for your suggestions! :)


Unfortunately, I've only just started looking at using PHP, so I'm unsure how to go about the whole getpage command by myself that you suggested. :(

Another helpful smart person on the net showed me this code to use for the navigation:


$pinfo = pathinfo($_SERVER["SCRIPT_FILENAME"]);
$reqpath = dirname($_SERVER["REQUEST_URI"]);

if(preg_match("/(.*?)(\d+)\.php/", $pinfo["basename"], $matches)) {
$fnbase = $matches[1];
$fndir = $pinfo["dirname"];
$current = intval($matches[2]);
$next = $current + 1;
$prior = $current - 1;
$next_file = $fndir . DIRECTORY_SEPARATOR . $fnbase . $next . ".php";
$prior_file = $fndir . DIRECTORY_SEPARATOR . $fnbase . $prior . ".php";

if(!file_exists($next_file)) $next_file = false;
if(!file_exists($prior_file)) $prior_file = false;


if($prior_file) {
$link = $reqpath . DIRECTORY_SEPARATOR . basename($prior_file);

echo "<a href=\"$link\">Prior</a>";
}

if($prior_file && $next_file) {
echo " / ";
}

if($next_file) {
$link = $reqpath . DIRECTORY_SEPARATOR . basename($next_file);

echo "<a href=\"$link\">Next</a>";
}
}

It seems to work well and does what I want - posting the code just in case another person somewhere is looking for a similar thing! :)

The way I've got my pages set up now is <?php include 'header.php' ?> at the top then <?php include 'footer.php' ?> at the bottom, which also includes the above code - I will have the 80 pages with their seperate content (jquery slideshows and text) and these includes - so if I need to make any major changes, the header and footer can easily get updated.

Do you think this is a good solution for someone not using a database/cms? :)
It's a shame I'm such a n00b at php and couldn't follow your suggestions lol, they sounded like they would work a lot better!

djr33
10-12-2011, 07:28 PM
There is nothing particularly complicated about what I explained, but of course you do need to generally understand PHP to use any of it. So my suggestion is simple: practice and be patient. Take everything one step at a time and you'll eventually get there.

There's no point in trying to find a shortcut when the problem is that you at the moment just don't know how to do what you're trying to do. We've all been there, but there's no way around it.


Honestly, if you aren't using a method that dynamically generates all of the pages from a single page -- in other words, you have one file for each page -- then there is no real point in trying to automate those links. Just cut and paste your link code and get in a rhythm with it. Then change the numbers. Changing those numbers is NOT difficult, and with just 80 pages you won't save any time even thinking about another solution, unless that solution will overall save you more time in other ways, because unless you decide to create a single file that generates all of those pages (that would save you time), then you will STILL at the very least need to cut and paste one line of code into each (to add the footer.php to each), so I don't see why you don't just cut and paste your links.
(I'm not generally recommending the cut and paste method, but if you aren't yet ready for the better solution, then that's probably the easiest/fastest temporary solution until you're ready for something more complex.)