Jump to content

Simple PHP Pagination


verbazon

Recommended Posts

Hi, i'm trying to get a small php pagination script to work with my news system. I followed a tutorial from a website callled biorust here: http://biorust.com/index.php?page=tutorial_detail&tutid=103"

 

The only thing that works is it's correctly limiting my news posts to display 3 at once. But the previous and next page links don't work. They show up as

http://mysite.com/index.php?start=3 (Previous Page)

http://mysite.com/index.php?start=-3 (Next Page)

 

and they don't do anything when clicked on. How can I make it so they display a new page with the next 3 news posts? Here is the code:

 

<?php

if ( $_GET['number'] == NULL ){

$limit = 3;

} else {

$limit = $_GET['number'];

}

 

if ($_GET['page']==NULL){

$start = 0;

} else {

$start = $_GET['page'];

}

 

// the if above, states that if the variable in the URL named page has a value, $start will be set to it, otherwise by default it is set to 0, meaning that it will start at the beginning of the table by default.

 

if($_GET['page'] < 0){

header("Location: http://mysite.com/index.php?page=0&limit=".$limit);

}

 

// this if above checks that the start variable isn’t negative, because if it is, our script will screw up. If it finds that it is, it redirects to the default page where the start is 0.

 

 

$query = mysql_query("SELECT * FROM `mytable` ORDER BY `id` DESC LIMIT ".$start.",".$limit.";") or die('MySQL Error: ' . mysql_error());

while($row = mysql_fetch_array($query))

{

echo '<h2>' . $row['title'] . '</h2>

<br />

<span style="margin-left:5px;" />Author: ' . $row['author'] . '<br />

<span style="margin-left:5px;" />Date: ' . $row['date'] . '

<p>' . nl2br($row['content']) . '</p>';

}

 

$previous = $start + $limit;

$next = $start - $limit;

echo "<div align='center'>

<a href='http://mysite.com/index.php?start=".$previous."'>Previous Page</a> -

<a href='http://mysite.com/index.php?start=".$next."'>Next Page</a>

</div><br />";

 

// the set of statements above displays the previous and next page links

?>

Link to comment
Share on other sites

Hi, from what I can see, you need to replace

<a href='http://mysite.com/index.php?start=".$previous."'>Previous Page[/url] -
   <a href='http://mysite.com/index.php?start=".$next."'>Next Page[/url]

 

with

<a href='http://mysite.com/index.php?start=".$previous."'>Previous Page</a> -
   <a href='http://mysite.com/index.php?start=".$next."'>Next Page</a>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.