1

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

I have 80 php pages (mostly just static html, I am using the php include command for the header and footer) - 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.

Anyone have any ideas how to do this? :)

*I'm a beginner coder and don't have enough time to learn how to set up a database/cms for this school project.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Jess
  • 487
  • 2
  • 12
  • 26
  • Do you have knowledge about paging..? if yes do that..:) – Chandresh M Oct 12 '11 at 05:04
  • I've had a play with some jquery/ajax paging? Isn't it more catered towards just plain data? Sorry if I've misunderstood you ^^; Is there an example website link that you could show me? I would really appreciate it :) – Jess Oct 12 '11 at 05:20
  • hi, you can refer this link if needed...[http://www.script-tutorials.com/demos/35/index.html](http://www.script-tutorials.com/demos/35/index.html). – Chandresh M Oct 12 '11 at 05:34
  • Thank you for posting a link! :) but yeah I don't think it's what I'm after unfortunately – Jess Oct 12 '11 at 07:10

3 Answers3

3

This is a relatively robust solution (considering the requirements):

$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 checks if the file next/prior actually exists
  • It supports multiple enumerations like {bla1, bla2, bla3} and {foo1, foo2, foo3}
vstm
  • 12,407
  • 1
  • 51
  • 47
  • Definitely much better than my solution ;) – Brendan Long Oct 12 '11 at 05:28
  • @BrendanLong: thank you very much! But it's just a thousand times more complicated :-\. And your points are still valid: it's also sensitive to filename changes. – vstm Oct 12 '11 at 05:41
  • Thank you very much for posting a solution! :) But I'm kinda unsure how to make this work lol ^^;; I tried pasting the code in the body where I want the links to be, but when I test it using a local server, no links show up. I'm sure I'm doing it totally wrong ><; – Jess Oct 12 '11 at 05:56
  • Well the filenames of the other pages have to be like `page1.php, page2.php, ... pageN.php` and they all have to be in the same directory. Then it should work. Also: This code could also go in your footer otherwise you have to either copy it into every file or put it in a file named "navigation.php" and include that navigation.php in every file. – vstm Oct 12 '11 at 06:07
  • Hmm so do I need to change the ["dirname"] to my directory? And the echos to ? ^^;; – Jess Oct 12 '11 at 06:19
  • 1
    Hmm no, the directory is automatically determined, it should work without change. – vstm Oct 12 '11 at 06:24
2

You could do something horrible like this:

// Get the current file name
$currentFile = $_SERVER["SCRIPT_NAME"];
$currentNumber = preg_replace('/\D/', '', $currentFile);
$next = $currentNumber + 1;
echo "<a href='page$next.php'>next page</a>";

Something similar could be used to find the previous page.

It's probably not a good idea though for the following reasons:

  • The page names are still hard-coded as page$next.php
  • If the page ID's have any gaps, you'll be directing users to 404's
  • If the pages are renamed, this will break horribly
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • Oh its not a good idea using php for this? What would you suggest, if you don't mind? :) – Jess Oct 12 '11 at 05:09
  • @Jess - I would suggest hard-coding the next value since the pages themselves are hard-coded. If the pages were in a database, I'd say use their database ID's to figure out the next page. The solution I posted will probably work, but it'll break if (1) the files are renamed or (2) the IDs have any gaps. – Brendan Long Oct 12 '11 at 05:12
0

I think you can check bellow code. It's simple.

<div>
<?php
$maxpage = 80;
if(!isset($_SESSION["currentPage"]))
    $_SESSION["currentPage"] = 0;

if($_SESSION["currentPage"] > 1)
{
?>
<a href="page<?php echo ($_SESSION["currentPage"] -1); ?>.php">Previous </a>
<?php
}
if ($_SESSION["currentPage"] < $maxpage )
{
?>
<a href="page<?php echo ($_SESSION["currentPage"] +1); ?>.php">Next </a>
<?php
}
?>
</div>

Hope this will help you.

Prasad.

Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52