Home  >  Article  >  Backend Development  >  php previous and next article code_PHP tutorial

php previous and next article code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:58:34936browse

php tutorial Previous and next article code
Basic knowledge:

It is to sort the id pairs by order by id desc or order by id asc, and then determine which ones are greater than the current id> or smaller than the current article id
The sql statement of the example 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`
--

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 data in the table `string_find`
--

INSERT INTO `string_find` (`id`, `charList`) VALUES
(1, 'Deng Pioneer'),
(2, 'Deng Pioneer'),
(5, 'fdafdsaf'),
(6, 'www.bKjia.c0m');

Okay, everything is ready, let’s take a look at the operation method
*/


mysql tutorial_pconnect('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 article".$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";
}

Original articles on this site are reproduced from www.bKjia.c0m

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631999.htmlTechArticlephp tutorial previous and next articles Basic knowledge of code: Just order by id desc or Order by id asc to sort, and then judge the ones that are greater than the current id or smaller than the current article id...
Statement:
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