PHP - Prev&下一个链接滚动数组

时间:2014-04-29 12:03:37

标签: php arrays

这是我的代码,但它没有显示Prev&下一个链接?

$path = basename($_SERVER['PHP_SELF']);

 foreach ($url as $key => $page)
 {
    if ($path == $page)
    {
          if ($prev = $url[$key-1])
                $prev_link = "<a href=\"{$prev}\">prev</a>";
          if ($next = $url[$key+1])
                $next_link = "<a href=\"{$next}\">next</a>";
    }
    else
    { }
    }
}

echo $prev_link . ' - ' . $next_link;

1 个答案:

答案 0 :(得分:1)

//i used my own urls from existing project, you may use your own
$url = array('/english/take-credit/1',
        '/english/take-credit/2', '/english/take-credit/3', '/english/take-credit/4',
        '/english/take-credit/5', '/english/take-credit/6', '/english/take-credit/7', '/english/take-credit/8');

//i use a framework so $path = basename($_SERVER['PHP_SELF']); was returning always "index.php"
    $path = $_SERVER['REQUEST_URI'];

$prev_link = $next_link = 'no more pages';

foreach ($url as $key => $page) {
    if ($path == $page) {
        if (isset($url[$key - 1]))
            $prev_link = "<a href=\"{$url[$key - 1]}\">prev</a>";
        if (isset($url[$key + 1]))
            $next_link = "<a href=\"{$url[$key + 1]}\">next</a>";
    }
}

echo $prev_link . ' - ' . $next_link;

此代码已在实时项目中测试过,如果您遇到更多困难,可以添加评论。