...

View Full Version : Pagination...or rather not!



paddy100
02-11-2010, 12:06 PM
:confused:

Hi Guys,

Thought I had finally nailed this but no..sniff

Coded below to paginate ( left at 2 items per page to start with just to test) and i thought I had cracked it...alas!

[CODE]
<?php

include("rhino.inc");
// Connects to your Database
$connection = mysql_connect($host, $user, $password)
or die ("couldn't connect to server");
$db = mysql_select_db($database, $connection)
or die ("couldn't connect to database");

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM `news` ORDER BY `date` DESC") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 2;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isnt below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM `news` ORDER BY `date` DESC $max") or die(mysql_error());

//This is where you display your query result

while ($news = mysql_fetch_array( $data_p )) {
$news_id = $news[urn];
$news_date = stripslashes($news[date]);
$news_author = stripslashes($news[author]);
$news_title = stripslashes($news[title]);
$news_snippet = stripslashes($news[snippet]);
{
Print $info['Name'];
echo "<table border='0' width='550'>";


echo "<tr>
<td>$news_title</td>
</tr>
<tr>
<td> date added $news_date by $news_author </td>
</tr>

<tr>
<td>$snippet</td>
</tr>
<tr>
<td><a href=\"shownews.php?news_id=$news_id\"><img src='/images/clickhereformore.png' width='185' height='25' border='0' /></a></td>
</tr>
<tr><td><hr></td></tr>";
}
echo "</table>";
}
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we dont need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
[CODE]


Whilst it does only display 2 items and has the right number of pages ( 4) when i go to page 2 etc the records displayed do not change =(

May be a simple thing but i cant get it!

in case anyone wants to know what i mean a current url is

www.hainaultbulldogs.co.uk/document2.php

Thanks

abduraooft
02-11-2010, 12:57 PM
if (!(isset($pagenum)))
{
$pagenum = 1;
} Are you relying on register_globals (http://php.net/manual/en/security.globals.php)? Try
if (!isset($_GET['pagenum']))
{
$pagenum = 1;
}
else
$pagenum =(int) $_GET['pagenum'] ; instead.

paddy100
02-11-2010, 01:40 PM
abduraooft I loves ya! lol

cheers for that! was going mad!

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum