Php previous article and next article implementation code and principles _ PHP Tutorial

Source: Internet
Author: User
Php code and principles described in the previous article and the next article. Implementation principle: sorts the id pairs by orderbyiddesc or orderbyidasc, and then judges the articles in the same column whose id is smaller than the current idor. The implementation principle of instance SQL statements is as follows:

Order by id desc or order by id asc for the id pair, and then judge the articles in the same column than the current id> or smaller than the current article id.
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.

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.

The 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

Sort is to sort the id by id desc or order by id asc, and then judge the articles in the same column than the current id or smaller than the current article id. The SQL statement of the instance is as follows...

Related Article

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.