Heim  >  Artikel  >  Backend-Entwicklung  >  PHP实现内容页面的上一篇下一篇的功能

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

WBOY
WBOYOriginal
2016-06-20 13:03:081293Durchsuche

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

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;
}

 


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn