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