PHP Simple Implementation Previous page next page Function Example _php tips

Source: Internet
Author: User

This article illustrates a simple PHP implementation of the previous page next page functionality. Share to everyone for your reference, specific as follows:

Idea Finishing:

Now a lot of people with ID 1 and minus 1 to achieve the previous and next, but the article ID will not be broken? So you need to know what the last ID and ID are OK.

So how to solve this problem, very simple!

Example:

If the ID200 of this article

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

If you're implementing the previous article, write the function on the Action=up page.

$id = $_get[' id '];
Previous:
$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, the query is smaller than the current ID (where ID < '. $id. ' Previous ' and 1 (limit 0,1) data larger than the current ID (where ID > '. $id. ' next), displayed in descending order (DESC, previous) and ascending (ASC, Next) , you can omit the descending or ascending order when you take only one article.

Specific implementation code: note the need to pass parameters

Front desk in the previous article, the next call:

<?php Echo Getprenext (Pre,news,$_request[catid],$_request[id]);? >//Display previous next 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");//id the most recent $nextR =mysql_fetch_array that is smaller than the incoming ID (MySQL  _query ("Select * from", $table. "Where catid=". $catid. "And id> $id ORDER by ID ASC limit 0,1");//id is the most recent one that is larger than the incoming ID $next = (Is_array ($nextR)?
  "Where id={$nextR [' ID ']}": ' Where 1>2 '); $pre = (Is_array ($preR)?
   "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: <a href= ' newsshow.php?id= '. $preR [' ID ']." &&catid= ". $catid." ' > ". $PreNext [' title ']."
      </a> ";
      else {echo $PreNext [' pre '] = ' previous: no '; } if ($PreNext =mYsql_fetch_array ($nextRow)) {echo $PreNext [' next '] = ' Next: <a href= ' newsshow.php?id= '. $nextR [' id ']. " &&catid= ". $catid." ' > ". $PreNext [' title ']."
      </a> ";
      else {echo $PreNext [' next '] = "Next: No";

 }
}

Code is tested and available

More about PHP Interested readers can view the site topics: "PHP common Database Operation skills Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator usage Summary" and "PHP string (String) Usage Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.