根据亨特F的回答,我的问题的解决方案几乎完成了。只需要进行一些调整。
我稍微修改了代码并在此处提交了一个新问题:需要 php 数组帮助 - 如果当前数组项 ="最后一项或第一项",则"做某事"
原文:
我希望能够创建一个简单的导航栏,其中包含 PREV 和 NEXT 链接,我可以使用它来循环浏览页面列表。导航栏将是所有要循环的页面中的 php 包含。
所以我想起点是创建一个需要循环的页面数组,尽管有 PREV NEXT 链接。
如。。。。
$projectlist = array(
'http://domain.com/monkey/',
'http://domain.com/tiger/',
'http://domain.com/banana/',
'http://domain.com/parrot/',
'http://domain.com/aeroplane/',
);
我想选择重新排序,添加或删除链接。因此,拥有这样一个自包含的主数组对我来说似乎是一个合乎逻辑的选择,因为我只需要更新这个列表即可进行任何未来的添加。
链接到的每个目录都有自己的索引.php文件,所以我从链接的末尾删除了索引.php部分,因为它不需要......还是吗?
。对于如何从这里继续,我很困惑。
我想我需要找出我当前所在的数组中的哪个页面,然后基于此生成 PREV 和 NEXT 链接。因此,如果我从"http://domain.com/parrot/"输入,则需要指向相关 PREV 和 NEXT 页面的链接。
任何帮助或信息,以指导我进入下一阶段,将不胜感激。
$currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables!
$currentPath = $currentPath[0]; //grab just the path
$projectlist = array(
'/monkey/',
'/tiger/',
'/banana/',
'/parrot/',
'/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) { //if it's on the first page, we want them to go to the last page
$prevlink = '<a href="'.$projectlist[ sizeof($projectlist)-1].'">Prev</a>';
} else { //otherwise just go to the n-1th page
$prevlink = '<a href="'.$projectlist[$currentPageIndex-1].'">Prev</a>';
}
if($currentPageIndex == sizeof($projectlist)-1 ) { //if we're on the last page, have them go to the first page for "next"
$nextlink = '<a href="'.$currentPageIndex[0].'">Next</a>';
} else {
$nextlink = '<a href="'.$projectlist[$currentPageIndex+1].'">Next</a>';
}
您可能需要考虑的一件事是在链接中对 href 目标进行编码。