PHP实现内容页面的上一篇下一篇的功能

WBOY
Release: 2016-06-20 13:03:08
Original
1323 people have browsed it

在内容站或者产品站中,为了更好的用户体验,一般在内容页面中会给出上一篇,下一篇的链接

PHP实现内容页面的上一篇下一篇的功能的代码

$previous 为上一篇的id

$next_item 为下一篇的id

$id_array = array();//获取该分类按指定排序得到的内容id数组

//获取上一篇,下一篇的id
if (is_array($id_array)) {
    reset ($id_array);
    $counter = 0;
    foreach ($id_array as $key => $value) {
      if ($value == (int)$_GET['products_id']) {
        $position = $counter;
        if ($key == 0) {
          $previous = -1; // it was the first to be found
        } else {
          $previous = $id_array[$key - 1];
        }
        if (isset($id_array[$key + 1]) && $id_array[$key + 1]) {
          $next_item = $id_array[$key + 1];
        } else {
          $next_item = $id_array[0];
        }
      }
      $last = $value;
      $counter++;
    }

    if ($previous == -1) $previous = $last;
}
Copy after login

 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template