开发者

Previous/next page links using PHP

开发者 https://www.devze.com 2023-04-12 04:38 出处:网络
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/b

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.


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}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消