Home > Article > Backend Development > php previous and next article code_PHP tutorial
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