简体   繁体   English

如何在每个页面上显示5个带有下一个和上一个按钮功能的帖子?

[英]How to show 5 post each page with next & previous button feature?

I have a page named 'job.php' , currently this page is showing all posted job. 我有一个名为'job.php'的页面,当前此页面显示所有已发布的作业。 But now I want to show only 5 latest posts. 但是现在我只想显示5个最新帖子。 And if anyone want to check the previous posts, they can click next button thus more 5 posts will be seen. 如果有人想查看以前的帖子,则可以单击“下一步”按钮,这样可以看到更多5个帖子。 There should be a previous button too. 也应该有一个上一个按钮。

Following is my code: 以下是我的代码:

$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC");
$num_row = mysql_num_rows($result1);

while($row1 = mysql_fetch_array($result1)){
     $cat=$row1['Category'];
     $title=$row1['Title'];
     echo "Job field: $cat<br/> Title: $title<br/>";
}

N:B: It's not pagination. N:B:这不是分页。 I don't want to show page numbers, just want to show next & previous button. 我不想显示页码,只想显示下一个和上一个按钮。

There are 100s of articles available on the Internet 互联网上有100篇文章

If you want to do on your own: 如果您想自己做:

  • Use LIMIT keywords in your query. 在查询中使用LIMIT关键字。
  • Pass the page and the multiplier to the LIMIT . page和乘数传递到LIMIT

Some code 一些代码

<?php
  $limit = 5;
  $start = (int)(($page - 1 ) * $limit);
  $page = mysql_real_escape_string($_GET["page"]);
  $query = "SELECT * FROM `table` LIMIT $start, {(int)$page + $limit}"
?>

There are two ways to achieve this. 有两种方法可以实现此目的。

1) In the query itself by using LIMIT 1)在查询中使用LIMIT

$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC LIMIT 1, 5");

2 ) By using loop 2)通过循环

$i = 0;
while($row1 = mysql_fetch_array($result1)){
    if($i < 5) {
       $cat=$row1['Category'];
       $title=$row1['Title'];
       echo "Job field: $cat<br/> Title: $title<br/>";
       $i++;
    }
}

You can pass the current value to URL and get it back by using $_GET.. 您可以将当前值传递到URL,然后使用$ _GET取回。

您可以使用以下查询:

Select * from table_name ORDER BY ID DESC LIMIT 5;

I feel its better to use pagination. 我觉得最好使用分页。 If you want dont want to show page nos, better hide it or just modify the code. 如果您不想显示页码,最好将其隐藏或仅修改代码。 If you planning to do it manually its a bit messy 如果您打算手动进行操作,会有些混乱

您可以在mysql查询中使用LIMIT:

$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC LIMIT 0, 5");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM