简体   繁体   中英

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. But now I want to show only 5 latest posts. And if anyone want to check the previous posts, they can click next button thus more 5 posts will be seen. 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. I don't want to show page numbers, just want to show next & previous button.

There are 100s of articles available on the Internet

If you want to do on your own:

  • Use LIMIT keywords in your query.
  • Pass the page and the multiplier to the 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

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

2 ) By using loop

$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..

您可以使用以下查询:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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