How to get the previous page and next page of an article in php,
The example in this article describes the method of obtaining the previous page and next page of the article in PHP. Share it with everyone for your reference. The specific method is as follows:
Today I found a problem with the previous and next pages of a website. There was no problem with the previous page, but on the next page, I went directly to the latest article published by this channel. According to the principle, it should be before and after the ID of the article itself. ID is the upper and lower pages, let me tell you in detail below.
Let’s look at an example first: 1,2,3,4,5.
If the above 5 are IDs, I need to use order by id desc to sort the previous page. For example, if my current ID is 3, then the number that comes out is 2. For the next page, we need to use order by id asc. The resulting ranking should be 4. Well, the principle is that simple.
SQL implementation method, the code is as follows:
Previous page:
Copy code The code is as follows:
$sql ="Select field from table name where id<3 order by id desc limit 1";
Next page:
Copy code The code is as follows:
$sql ="Select field from table name where id>3 order by id asc limit 1";
The result is the same as what we thought. Finally, I will share with you a function I wrote before. The code is as follows:
Copy code The code is as follows:
/*
Previous page, next page
int $tag 0 previous page, 1 next page
int $fid
*/
function nextPre($tag=0,$zid,$fid)
{
if( $tag )
{
$sql="Select field from table name where id<$fid order by id desc limit 1";
}
else
{
$sql="Select field from table name where id>$fid order by id asc limit 1";
}
//$sql = "Select * from table name where order by id desc limit 0,10";
$result = mysql_query($sql) or die('query error');
if( mysql_num_rows( $result ) )
{
$rs = mysql_fetch_array( $result );
Return "
".$rs['title']."";
}
else
{
Return 'No more';
}
}
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/920627.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920627.htmlTechArticleHow to get the previous page and next page of an article in php. This article explains how to get the previous page and next page of an article in php Methods on the next page. Share it with everyone for your reference. The specific method is as follows: Post today...