我创建了一个 PHP 博客。除了一个小故障,一切都正常运行。我想按日期降序显示文章。因此,每当将文章添加到数据库时,自动时间戳就会记录插入时间。
GLITCH -> 我想每页显示 5 篇文章。我通过使用这个
轻松地为第一页做到了这一点$sql="SELECT id,article_name,article_body,date
from articles order by date desc limit 5" ;
现在我想继续第二页,并希望第二页显示第 1 页离开的文章。如果按降序排列有 10 篇文章,则第 1 页应显示前 5 篇,第 2 页应显示后 5 篇。
当每天添加许多文章时,此逻辑应该实时工作。我使用了这个查询,但它只显示了 1 行。
$sql="select id,article_name,article_body,unix_timestamp(date)
from articles
where date < (select unix_timestamp(date)
from articles order by date desc limit $n,1 )
order by date desc limit $n,5" ;
$n - 从中提取行的 ID。
最佳答案
利用sql中的偏移量:
$items_per_page=5;
$offset=($current_page-1)* $items_per_page;
SQL:
SELECT id,article_name,article_body,date
from articles order by date desc LIMIT $items_per_page OFFSET $offset
关于php - 小于号的MySQL时间戳比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11840099/