PHP previous article, next article implementation code and principles

Source: Internet
Author: User

Implementation principle:

Order by id desc or order by id asc for the ID pair, and then judge whether it is earlier than the current ID> or than the current IDArticleId.
The SQL statement of the instance is as follows:

$ ID is the ID of a face-to-face document.

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'
--

Create Table if not exists 'string _ Find '(
'Id' int (4) not null auto_increment,
'Charlist' varchar (100) default null,
Primary Key ('id ')
) Engine = MyISAM default charset = utf8 auto_increment = 7;

--
-- Export the data in the table 'string _ Find'
--

Insert into 'string _ Find '('id', 'charlist') Values
(1, 'foot home '),
(2, 'baidu '),
(5, 'www .baidu.com '),
(6, 'www .jb51.net ');

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

CopyCode The Code is as follows: mysql_connect ('localhost', 'root', 'root') or die (mysql_error ());
Mysql_select_db ('cc ');
Mysql_query ("set names 'gbk '");
$ Cid = 5; // The number of your current article
$ SQL = "select * From string_find where ID> $ CID order by ID DESC limit 0, 1"; // previous article
$ Sql1 = "select * From string_find where ID <$ CID order by id asc limit 0, 1"; // next article

$ Result = mysql_query ($ SQL );
If (mysql_num_rows ($ result ))
{
$ Rs = mysql_fetch_array ($ result );
Echo "previous". $ Rs [0];
}
Else
{
Echo "no ";
}

$ Result1 = mysql_query ($ sql1 );
If (mysql_num_rows ($ result1 ))
{
$ RS1 = mysql_fetch_array ($ result1 );
Echo "Next". $ RS1 [0];
}
Else
{
Echo "no ";
}

The following is an article written by other netizens.
Because I want a visitor to view the title of the previous topic and the next topic when browsing the webpage, it must be queried in the database and can be obtained through the limit, for example, if my blog is automatically incremental by ID, you can find the value greater than or less than the current ID.

$ Upsql = "select * From 'blog 'where 'id' <$ id order by 'id' DESC limit 0, 1 ";
$ Downsql = "select 'id', 'title' from 'blog 'where 'id'> $ id order by 'id' ASC limit 0, 1 ";

Then retrieve the data through the query.
If it is just a single "previous", "Next", there is no need to query, so you do not have to query, but you may see it after clicking, this is already the homepage or the last page.Copy codeThe Code is as follows: Switch ($ Act ){
Case "up ":
$ SQL = "select * From 'blog 'where 'id' <$ id order by 'id' DESC limit 0, 1 ";
Break;
Case 'lowdown ':
$ SQL = "select * From 'blog 'where 'id'> $ id order by 'id' ASC limit 0, 1 ";
Break;
Default:
$ SQL = "select * From 'blog 'where 'id' = $ id limit 0, 1 ";
Break;
}

Implement the previous topic and the next topic by passing an action

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.