我正在展示数据库中最新的10个话题和最老的10个话题。我能够在最新的10个话题中展示我需要的内容。我的问题是,当数据库中有21个话题时,最后的10个话题按照我想要的方式显示,但当数据库中只有20个话题时,最新的最后一个话题仍然会出现在最老的话题中。为了更清楚,请参考以下图片。
- 当数据库中有21个或更多话题时,情况如下 -
- 当数据库中有20个话题时,情况如下 -
我不希望像当有20个话题时那样重复出现某个话题。这是我从数据库中获取最后10个话题的代码:
- 当数据库中有21个或更多话题时,情况如下 -
![This is what happen when I have 21 or more topics in database](https://istack.dev59.com/FbDoO.webp)
![This is what happen when I have 20 topics in database](https://istack.dev59.com/SPs3J.webp)
// fetching last 10 topics from forum
function history() {
$sql = "SELECT * FROM (
SELECT f_id AS id, f_title AS name,f_dept AS dept,f_last_view AS last_view
FROM forum
ORDER BY last_view ASC
LIMIT 10 OFFSET 1
) AS `table` ORDER by last_view DESC ";
//-run the query against the mysql query function
$result=mysql_query($sql) or die(mysql_error());
$history = array();
//-create while loop and loop through result set
while (($row = mysql_fetch_assoc($result)) !== false){
$history[] = array(
'id' => $row['id'],
'name' => $row['name'],
'dept' => $row['dept'],
'last_view' => $row['last_view'],
);
}
return $history;
}
提示:我知道mysql_*已经不推荐使用,但请原谅我的使用。预先感谢您的理解。