skip to Main Content

I have 100 of php pages and all are just static pages. I wanted to have next/prev page link in each and every page footer that should work automatically rather than setting up manually. All pages will be having the URLs like "components/button", "components/tooltip", "design/tokens", and "Get started"

Same like what Carbon Design System has in the footer.

I wanted to have an easier approach for linking automatically for each component page.

I tried some jQuery but not getting the required results.

2

Answers


  1. Here is a very simple solution I’ve put together. You just need to add each of the pages you want included into the PHP array, and then include the script on each page. Pages are ordered based on their position within the array.

    footer-navigation.php:

    <?php
    // Array of pages in the order they should appear
    $pages = [
        '/test.php',
        '/files/file1.php',
        '/files/file2.php'
    ];
    
    $currentUrl = $_SERVER['REQUEST_URI'];
    $currentIndex = array_search($currentUrl, $pages);
    $prevIndex = $currentIndex > 0 ? $currentIndex - 1 : null;
    $nextIndex = $currentIndex < count($pages) - 1 ? $currentIndex + 1 : null;
    $prevUrl = $prevIndex !== null ? $pages[$prevIndex] : '#';
    $nextUrl = $nextIndex !== null ? $pages[$nextIndex] : '#';
    ?>
    <footer>
        <?php if($prevIndex !== null): ?>
            <a href="<?php echo $prevUrl; ?>">Previous</a>
        <?php endif; ?>
    
        <?php if($nextIndex !== null): ?>
            <a href="<?php echo $nextUrl; ?>">Next</a>
        <?php endif; ?>
    </footer>
    

    To add the navigation to each page, place the following in your footer:

    <?php include 'footer-navigation.php'; ?>
    

    Make sure the path is correct for pages not inside the same directory as footer-navigation.php.

    Login or Signup to reply.
  2. This seems to be a duplicate thread, please check out this article.

    However, I will still try my hand at this.. You could try to assign your urls to an array… (or even a database).

    inside array.inc.php:

       <?php
        $array = [
               "1" => "example.com/components/example1",
               "2" => "example.com/components/example2",
         ];
    

    inside the footer.php file:

       <?php
         require_once "array.inc.php";
         // find the current page number from the array.
         $_SESSION["page"] = intval(array_search($_SERVER['PHP_SELF'], $array));
        ?>
    
    <button><a href="nextpage.inc.php">next page</a></button>
    

    Then, inside a redirect page, nextpage.inc.php, we combine the array into the url:

     <?php
            require_once "array.inc.php";
            $_SESSION['page']++;
            header("location: " . $array[$_SESSION['page']]);
     ?>
    

    Previous page is the same, except reduce the $_SESSION["page"] value by 1, and error check for page number less than or equal to "0".

    Hope this helps…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search