PHP last article the next article implementation code and principles

  • 2020-03-31 20:39:40
  • OfStack

Realization principle:

Order by id desc or order by id asc for the id pair, and then determine the ratio of the current id> Or is less than the current article id of the same column of the article.
The SQL statement for the instance is as follows:

The $id is the id of the face to face 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

--
-- the structure 'string_find' of the table
--

CREATE TABLE IF NOT EXISTS 'string_find' (
'id' int(4) NOT NULL auto_increment,
The default NULL ` charList ` varchar (100),
PRIMARY KEY (` id `)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7;

--
-- export the data 'string_find' in the table
--

INSERT INTO 'string_find' (' id ', 'charList') VALUES
(1, 'this site '),
(2, "baidu"),
(5, 'www.baidu.com'),
(6, 'www.jb51.net');

All right, so now we have everything we need to do

 
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"; //Last post
$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 " In the previous ".$rs[0]; 
} 
else 
{ 
echo " Without the "; 
} 

$result1 = mysql_query( $sql1 ); 
if( mysql_num_rows( $result1 ) ) 
{ 
$rs1 = mysql_fetch_array( $result1 ); 
echo " The next article ".$rs1[0]; 
} 
else 
{ 
echo " Without the "; 
} 


The following are articles written by other netizens.
Since I want visitors to see the title of the previous topic and the next topic when browsing the web page, it must be searched out in the database, which can be retrieved by limit limit. For example, my blog is automatically incremented by ID, so it can be retrieved by finding the current ID is greater than or less than

$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";

Through the query, the data
If only a single "last "," next" then there is no need to query, so there is no need to query, but perhaps users will see after clicking, this is the home page or this is the last page, ha ha
 
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; 
} 

Implement the previous theme and the next theme by passing an action

Related articles: