Php 带有数组next()和prev()链接的图像导航
我正在尝试将此功能用于图像导航:Php 带有数组next()和prev()链接的图像导航,php,arrays,image,loops,navigation,Php,Arrays,Image,Loops,Navigation,我正在尝试将此功能用于图像导航: function array_key_relative($array, $current_key, $offset = 1, $strict = true) { // create key map $keys = array_keys($array); // find current key $current_key_index = array_search($current_key, $keys, $strict); // return d
function array_key_relative($array, $current_key, $offset = 1, $strict = true) {
// create key map
$keys = array_keys($array);
// find current key
$current_key_index = array_search($current_key, $keys, $strict);
// return desired offset, if in array, or false if not
if(isset($keys[$current_key_index + $offset])) {
return $keys[$current_key_index + $offset];
}
return false;
}
我想这样使用它:
<?php
$images = array();
foreach ($images as $key => $image)
$prev_key = $this->array_key_relative($images, $key, -1);
$next_key = $this->array_key_relative($images, $key, 1);
?>
<a href="<?php echo "image?id=".$images[$prev_key]->id; ?>">Prev</a>
<a href="<?php echo "image?id=".$images[$next_key]->id; ?>">Next</a>
问题是,当我按下下一步或上一步时,它只会工作一次,因此,例如,如果当前键为1,如果我按下下一步,将转到2,但一旦我进入2页,导航将停止工作(不会转到3、4、5等)。
有人能给我指出正确的方向吗?
谢谢。您的下一个\u键在第一次查看时没有包含+符号- $next\u key=$this->array\u key\u relative($images,$key,+1) 但我会用
$next = $images++;
$prev = $images--;
$next_key = $this->array_key_relative($images, $key, $next);
$prev_key = $this->array_key_relative($images, $key, $prev);