So I am trying to create a next and previous buttons on my website and I thought I approach it in a rather generic way. I named each of my pages like this:
1.php
2.php
3.php
and so on
So figuratively speaking, while on 2.php, the previous link would point to 1.php and the next link would point to 3.php.
To automate this process on all pages, I am trying to come up with a simple code to add 1 and minus 1 from the current filename.
So in PHP, I did this:
<?php
$dir = $_SERVER['PHP_SELF'];
$dirchunks = explode("/", $dir);
$current = $dirchunks[3];
?>
$current echos out 2.php, the page I am on, is there anyway to add 1 or minus 1 while keeping the .php intacted with the variable $current? so $current 2.php would change to 3.php and 1.php?
I hope my question wasn’t confusing and someone can shed some light on my situation. Thanks for the help!
Remember that there will a wraparound on the first and last pages, and you should use
basenameto get the name of the file, passing the extension as an optional argument will strip that off too, leaving just the number of the page: