问题描述
需要使用此脚本一点帮助。它的工作,但我喜欢的方式来简化它一点点。
目前,我有'如果($ currentPageIndex == 4)我有我加入'4'每次都手动更新或从projectlist数组中删除的项目。我需要的是好歹说的:如果($ currentPageIndex == 阵列的最后一个项目)这样的话,我可以添加/删除项目,而不必担心更新序号。
我应该如何去吗?我已经在各种选项念起来迄今为止尝试的事情了,没有运气。
另外,如果有可能将溶液中的preV&放大器可用;接下来的联系呢?
因此,而不是在 +4 和 -4 它跳到分别在第一和最后一个项目。
任何帮助非常AP preciated。
工作演示在这里:
code位置:
< PHP$ currentPath =爆炸(,$ _ SERVER ['REQUEST_URI']'?'); //确保我们不指望任何GET变量!
$ currentPath = $ currentPath [0]; //抢人的路
$ projectlist =阵列(
'/项目/猴/',
'/项目/虎/',
'/项目/鹦鹉/',
'/项目/香蕉/',
'/项目/飞机/',
);如果(!in_array($ currentPath,$ projectlist)){
死('不是一个有效的页面!'); //他们并没有在我们的主列表访问一个页面,在这里处理错误。
}$ currentPageIndex = array_search($ currentPath,$ projectlist);如果($ currentPageIndex大于0){
$prevlink ='&下; A HREF =。$ projectlist [$ currentPageIndex-1]。> preV&下; / A>';
}其他{
$prevlink ='&下; A HREF =。$ projectlist [$ currentPageIndex + 4]。> preV&下; / A>';
}如果($ currentPageIndex == 4){
$ NEXTLINK ='&下; A HREF =。$ projectlist [$ currentPageIndex-4]。>接着与下; / A>';
}其他{
$ NEXTLINK ='&下; A HREF =。$ projectlist [$ currentPageIndex + 1]。>接着与下; / A>';
}?>< UL ID =子导航><立GT;
< PHP
的print_r($prevlink);
?>
< /李><立GT;
< PHP
的print_r($ NEXTLINK);
?>
< /李>< / UL>
使用
的sizeof($ projectList) - 1
以获得所需的数
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
sizeof($projectList) - 1
to get the desired number
这篇关于需要PHP数组帮助 - 如果当前数组项='最后一个或第一个项目“,然后”做一些事情“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!