I made a PHP blog. Everything is up and running except one glitch. I want to display the articles in decreasing order sorted by date. So whenever an article is added to the DB an auto timestamp records the time of insertion.
我做了一个PHP博客。除了一个小故障外,一切都正常运行。我想按日期按降序显示文章。因此,无论何时将文章添加到数据库,自动时间戳都会记录插入时间。
GLITCH - > i want to display 5 articles per page. I did that easily for the first page by using this
GLITCH - >我想每页显示5篇文章。我通过使用它在第一页轻松地做到了这一点
$sql="SELECT id,article_name,article_body,date
from articles order by date desc limit 5" ;
Now i want to proceed with the second page and would like the second page to display the articles from where the page 1 left. If there are 10 articles in descending order, page 1 should display the first 5 and page 2 should display the next five.
现在我想继续第二页,并希望第二页显示第1页离开的文章。如果按降序排列10篇文章,则第1页应显示前5个,第2页应显示下5个。
This logic should work realtime when many articles are added per day. i used this query but it's displaying just 1 row.
当每天添加许多文章时,此逻辑应该实时工作。我使用了这个查询,但它只显示了一行。
$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 - the id from where the rows will be extracted.
$ n - 提取行的ID。
2 个解决方案
#1
2
utilize the offset in sql:
利用sql中的偏移量:
$items_per_page=5;
$offset=($current_page-1)* $items_per_page;
the sql:
sql:
SELECT id,article_name,article_body,date
from articles order by date desc LIMIT $items_per_page OFFSET $offset
#2
1
try:
尝试:
select id,article_name,article_body,unix_timestamp(date)
from articles
where date < ( select date from articles order by date desc limit $n,1 )
order by date desc limit $n,5
where $n should be the last id from previous page
其中$ n应该是上一页的最后一个id
#1
2
utilize the offset in sql:
利用sql中的偏移量:
$items_per_page=5;
$offset=($current_page-1)* $items_per_page;
the sql:
sql:
SELECT id,article_name,article_body,date
from articles order by date desc LIMIT $items_per_page OFFSET $offset
#2
1
try:
尝试:
select id,article_name,article_body,unix_timestamp(date)
from articles
where date < ( select date from articles order by date desc limit $n,1 )
order by date desc limit $n,5
where $n should be the last id from previous page
其中$ n应该是上一页的最后一个id