Need a little help with this script. It’s working but I’d love a way to simplify it a little.
At the moment I have ‘ if($currentPageIndex == 4) ‘ I have to manually update the ‘4’ every time I add or remove an item from the projectlist array. What I need is someway of saying ‘if($currentPageIndex == the last item of the array)’ That way I can add / remove items without worrying about updating the number as well.
How should I go about this? I’ve read up on various options and been trying things out with no luck so far.
Also if possible would the solution be usable on the Prev & Next links as well?
So instead of the +4 and -4 it jumps to the first and last item respectively.
Any help much appreciated.
Working demo here: http://www.ok-hybrid.com/projects/monkey/
Code here :
<?php
$currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables!
$currentPath = $currentPath[0]; //grab just the path
$projectlist = array(
'/projects/monkey/',
'/projects/tiger/',
'/projects/parrot/',
'/projects/banana/',
'/projects/aeroplane/',
);
if(! in_array($currentPath, $projectlist) ) {
die('Not a valid page!'); //they didn't access a page in our master list, handle error here.
}
$currentPageIndex = array_search($currentPath, $projectlist);
if($currentPageIndex > 0) {
$prevlink = '<a href="'.$projectlist[$currentPageIndex-1].'">Prev</a>';
} else {
$prevlink = '<a href="'.$projectlist[$currentPageIndex+4].'">Prev</a>';
}
if($currentPageIndex == 4) {
$nextlink = '<a href="'.$projectlist[$currentPageIndex-4].'">Next</a>';
} else {
$nextlink = '<a href="'.$projectlist[$currentPageIndex+1].'">Next</a>';
}
?>
<ul id="sub-nav">
<li>
<?php
print_r($prevlink);
?>
</li>
<li>
<?php
print_r($nextlink);
?>
</li>
</ul>
use
to get the desired number