Home > Backend Development > PHP Tutorial > php previous article, next article implementation code and principle explanation_PHP tutorial

php previous article, next article implementation code and principle explanation_PHP tutorial

WBOY
Release: 2016-07-21 15:38:04
Original
1042 people have browsed it

Implementation principle:

is to sort the id pairs by order by id desc or order by id asc, and then determine the articles in the same column that are > or smaller than the current article id.
The sql statement of the example is as follows:

$id is the id of the in-person article

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, 'Script Home'),
(2, 'baidu'),
(5, 'www.baidu.com'),
(6, 'www .jb51.net');

Now that everything is ready, let’s take a look at the operation method

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 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 post".$rs[0];
}
else
{
echo "No more";
}

$result1 = mysql_query( $sql1 );
if( mysql_num_rows( $result1 ) )
{
$rs1 = mysql_fetch_array( $result1 );
echo "Next article".$rs1[0];
}
else
{
echo "No more";
}


The following are articles written by other netizens.
Since I hope that visitors need to see the titles of the previous topic and the next topic when browsing the web, they must be queried in the database. They can be retrieved through limit restrictions. For example, my blog is According to the ID that is automatically incremented, you can get it by looking for something greater 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";

Re Through query, retrieve the data
If it is just a single "previous article" and "next article", then there is no need to query. In this case, there is no need to query, but maybe the user will see after clicking that this is already the homepage. Or this is already the last page, haha
Copy code 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 'Down' :
$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;
}

By passing an action to achieve the previous topic, next theme

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321805.htmlTechArticleImplementation principle: It is to sort the id pairs by order by id desc or order by id asc, and then judge the comparison Articles in the same column with the current ID or less than the current article ID. The sql statement of the example is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template