English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Example of Simple Implementation of Previous Page and Next Page Function in PHP

This article illustrates a simple implementation of the previous and next page functions in PHP. Shared for everyone's reference, as follows:

Thought organization:

Nowadays, many people use the increment of id1and subtraction1Implement the previous and next articles, but wouldn't the article ID break? So you need to know what the previous ID and the next ID are, that's all.

How to solve this problem? It's very simple!

Example:

Suppose this article's ID200

<a href="?action=up&id=200">Previous Article</a>
<a href="?action=down&id=200">Next Article</a>

If it is to implement the previous article, write the function on the page action=up

$id= $_GET['id'];
//Previous Article:
$sql = select * from article where id < '.$id.' order by id desc limit 0,1");
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs);
//Next:
$sql = select * from article where id < ${id} order by id asc limit 0,1");
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs);

Principle, query entries with an ID smaller (where id < ${id} previous) and larger (where id > ${id} next) than the current ID1entries (limit 0,1)Data, and displayed in descending order (desc, previous) and ascending order (asc, next). When only one entry is taken, descending or ascending order can be omitted.

Specific implementation code: Note that parameters need to be passed

The front-end calls the previous and next articles at:

<63;php
 echo GetPreNext(pre,news,$_REQUEST[catid],$_REQUEST[id]);?>
//Display previous and next articles
 function GetPreNext($gtype,$table,$catid,$id){
 $preR = mysql_fetch_array(mysql_query("select * from ".${table}" where catid=".${catid}" and id<${id} order by id desc limit 0,1");//The closest entry with an ID smaller than the input ID
 $nextR = mysql_fetch_array(mysql_query("select * from ".${table}" where catid=".${catid}" and id>${id} order by id asc limit 0,1");//The closest entry with an ID larger than the input ID
  $next = (is_array($nextR) &63; " where id={$nextR['id']} " : ' where' 1>2 ");
  $pre = (is_array($preR) &63; " where id={$preR['id']} " : ' where' 1>2 ");
   $query = "Select" * from ".${table}" ";
      $nextRow = mysql_query($query.$next);
      $preRow = mysql_query($query.$pre);
      if($PreNext=mysql_fetch_array($preRow))
      {
       echo $PreNext['pre'] = "Previous Article: <a href='newsshow.php63;id=".$preR['id']."&&catid=".$catid."'>".$PreNext['title']."</a> ";
      }
      else
      {
       echo $PreNext['pre'] = "Previous Article: None ";
      }
      if($PreNext=mysql_fetch_array($nextRow))
      {
       echo $PreNext['next'] = "Next Article: <a href='newsshow.php63;id=".$nextR['id']."&&catid=".$catid."'>".$PreNext['title']."</a> ";
      }
      else
      {
        echo $PreNext['next'] = "Next Article: None ";
      }
}

The code has been tested and is available.

Readers who are interested in more PHP-related content can check the special topics on this site: 'Summary of Common Database Operation Skills in PHP', 'Comprehensive Skills of PHP Array (Array) Operation', 'Summary of Sorting Algorithms in PHP', 'Summary of Common Traversal Algorithms and Skills in PHP', 'PHP Data Structures and Algorithms Tutorial', 'Summary of Algorithm Design in PHP', 'Summary of Mathematical Operation Skills in PHP', 'Summary of Regular Expression Usage in PHP', 'Summary of Operation and Operator Usage in PHP', and 'Summary of String (string) Usage in PHP'.

I hope the content described in this article will be helpful to everyone's PHP program design.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright. It has not been manually edited and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like