SO am am soon going to build a blog. what I would like to know is how to do the prvious posts button at the bottom of the page with like the 1|2|3...Next|Last that type I was just wondering how it is done I know to fill the Homepage your query would be like

$sql=select * from post limit 5 order by date asc

so when the person clicks for page two how does the php handle that and what is the sql is it like

$sql=select * from post limit(5, 5) order by date asc

to fetch the next five or what? and most importantly how to you make the PHP handler

7 Years
Discussion Span
Last Post by Agarsia

first get the number off all posts:

$number= mysql_num_rows(mysql_query("SELECT * FROM post"));

then do

$sites = ceil($number/5);

to get how many pages there are. (5 is the number of posts per page)
now test if $sites > 1, now if its over 1 use a for-loop to create the Navigation, like this:

if($sites>1) {
if(!isset($_GET['page'])) { if visiting the first page, there is no Previous
 echo ">Previous | ";
} else {
 echo "<a href='index.php?page=".($_GET['page']-1)."'>Previous</a> | ";
 echo "<a href='index.php'>1</a> | ";
 for($i=1; $i <$sites;$i++) {
  if($_GET['page']==$i {
   echo ($i+1)." | ";
   echo "<a href='index.php?page=".$i."'>".($i+1)."</a> | ";
if($_GET['page']!=($sites-1)) { //If you are visiting last page, there is no Next anymore
 echo "<a href='index.php?page=".($_GET['page']+1)."'>Next</a>";
} else {
 echo "Next";

this code should work


Intesting so then how would It display the contents of that page like if I clicked on page 5 how would it structure the SQl to get the specific page would it be

$page = $i*5
$sql = select * from posts limit($page,5)

where it would take the page number times it by 5 to get the post starting from that point and limit to 5?


use this at top of the page:

if(!isse($_GET['page')) { $limit = 0;}else { $limit = $_GET['page']*5; }

and the sql is everywhere on the page the same:

$sql = "SELECT * FROM posts LIMIT ".$limit.",5";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.