PHP on a, the next article implementation code and principle explanation _php skill

Source: Internet
Author: User
Implementation principle:

This is to sort the ID pair for the ORDER by id DESC or the orders by ID ASC, and then to judge the article that is more identical than the current id> or less than the current article ID.
The SQL statement for the instance is as follows:

$ID is the ID of the face article.

SELECT * FROM news where id< $id the ORDER by id DESC limit 0,1
SELECT * FROM news where id> $id the ORDER by id DESC limit 0,1

--
--The structure of the table ' String_find '
--

CREATE TABLE IF not EXISTS ' String_find ' (
' ID ' int (4) not NULL auto_increment,
' charList ' varchar 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, ' cloud-dwelling community '),
(2, ' Baidu '),
(5, ' www.baidu.com '),
(6, ' www.jb51.net ');

All right, let's see how it's done.

Copy Code code as follows:

mysql_connect (' localhost ', ' root ', ' root ') or Die (Mysql_error ());
mysql_select_db (' cc ');
mysql_query ("Set names ' GBK '");
$cid =5;//is the number of your current article
$sql = "SELECT * from String_find where id> $cid the ORDER by id DESC limit 0,1"; Previous article
$sql 1 = "SELECT * from String_find where id< $cid the 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";
}

$result 1 = mysql_query ($sql 1);
if (mysql_num_rows ($result 1))
{
$rs 1 = mysql_fetch_array ($result 1);
echo "Next". $rs 1[0];
}
Else
{
echo "No";
}


The following are the articles written by other netizens.
Because I want visitors to see the previous topic while browsing the web, the title of the next topic, so it must be queried in the database, can be taken by the limit limit, for example, my blog is automatically incremental by ID, you can find by looking at greater than or less than the current ID to fetch

$UpSQL = "SELECT * from ' blog ' WHERE ' id ' < $id ' id ' DESC LIMIT 0,1";
$DownSQL = "Select ' id ', ' Title ' from ' blog ' WHERE ' id ' > $id ' id ' ASC LIMIT 0,1";

And then through the query, take out the data
If only a single "previous", "next" then there is no need to query, this is not necessary to query, but perhaps the user will see after clicking, this is already the home page or this is the last page, hehe
Copy Code code as follows:

Switch ($act) {
Case "Up":
$SQL = "SELECT * from ' blog ' WHERE ' id ' < $id ' id ' DESC LIMIT 0,1";
Break
Case ' down ':
$SQL = "SELECT * from ' blogs ' WHERE ' id ' > $id order by ' ID ' ASC LIMIT 0,1";
Break
Default:
$SQL = "SELECT * from ' blog ' WHERE ' ID ' = $id LIMIT 0,1";
Break
}

To implement the previous topic by passing an action, the next topic

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.