Php previous and next article code

Source: Internet
Author: User
Basic knowledge: sorts the IDs by orderbyiddesc or orderbyidasc, and then judges that the id is smaller than the id of the current article than the current idor. the SQL statement of the instance is as follows:

Basic knowledge: sort the id by id desc or order by id asc, and then judge whether the current id is greater than or less than the current article id. the SQL statement of the instance is as follows:

Select * from news where id <$ id order by id desc limit 0, 1

Select * from news where id> $ id order by id desc limit 0, 1

Table structure 'string _ find'

  1. Create table if not exists 'string _ find '(
  2. 'Id' int (4) not null auto_increment,
  3. 'Charlist' varchar (100) default NULL,
  4. Primary key ('id ')
  5. ) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 7;

Export the data in the table 'string _ find'

  1. Insert into 'string _ find '('id', 'charlist') VALUES
  2. (1, 'pioneer Deng '),
  3. (2, 'pioneer Deng '),
  4. (5, 'fdafdsaf '),
  5. (6, 'www .phpfensi.com ');

Now everything is ready. let's take a look at the operation method below:

  1. */
  2. Mysql tutorial _ pconnect ('localhost', 'root', 'root') or die (mysql_error ());
  3. Mysql_select_db ('CC ');
  4. Mysql_query ("set names 'gbk '");
  5. $ Cid = 5; // The number of your current article
  6. $ SQL = "select * from string_find where id> $ cid order by id desc limit 0, 1"; // previous article
  7. $ Sql1 = "select * from string_find where id <$ cid order by id asc limit 0, 1"; // next article
  8.  
  9. $ Result = mysql_query ($ SQL );
  10. If (mysql_num_rows ($ result ))
  11. {
  12. $ Rs = mysql_fetch_array ($ result );
  13. Echo "previous". $ rs [0];
  14. }
  15. Else
  16. {
  17. Echo "no ";
  18. }
  19.  
  20. $ Result1 = mysql_query ($ sql1 );
  21. If (mysql_num_rows ($ result1 ))
  22. {
  23. $ Rs1 = mysql_fetch_array ($ result1 );
  24. Echo "next". $ rs1 [0];
  25. }
  26. Else
  27. {
  28. Echo "no ";
  29. }

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.