Can anyone please help sort out this pagination script.
Currently it works if i want to move through each of the pages seperately but what i want to do is to be able to have the 5 latest news articles listed by title and the viewer be able to click on one of the titles (links) and be taken straight to the page within the pagination. Say the user wanted to view the third article it would take them to page number 3.
I don't know how to do it and would appreciate some help
here is the code what actually works
<h2>News / Events</h2>
<?
//REMEMBER TO CONNECT TO DATABASE!
include_once("includes/connection.php");
//**EDIT TO YOUR TABLE NAME, ECT.
$t = mysql_query("SELECT * FROM `news`");
if(!$t) die(mysql_error());
$a = mysql_fetch_object($t);
$total_items = mysql_num_rows($t);
$limit = $_GET['limit'];
$type = $_GET['type'];
$page = $_GET['pagenum'];
//set default if: $limit is empty, non numerical, less than 1, greater than 50
if((!$limit) || (is_numeric($limit) == false) || ($limit < 2) || ($limit > 50)) {
$limit = 1; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}
//calcuate total pages
$total_pages = ceil($total_items / $limit);
$set_limit = $page * $limit - ($limit);
//query: **EDIT TO YOUR TABLE NAME, ECT.
$q = mysql_query("SELECT id,title,content, DATE_FORMAT(time, '%W %D %M %Y') as date, photo FROM news ORDER BY time DESC LIMIT $set_limit, $limit");
if(!$q) die(mysql_error());
$err = mysql_num_rows($q);
if($err == 0) die("No matches met your criteria.");
$numofrows = mysql_num_rows($q);
//Results per page: **EDIT LINK PATH**
echo("
<a href=?page=news&limit=10&pagenum=1></a>
<a href=?page=news&limit=25&pagenum=1></a>
<a href=?page=news&limit=50&pagenum=1></a>");
//show data matching query:
while($code = mysql_fetch_array($q)) {
echo "<div id=\"right\"><h3>".$code['title']."</h3><i>Date added: ".$code['date']."</i><p class=\"style3\"><img src=http://www.project-sw.co.uk/jack/images/".$code['photo'].">".nl2br($code['content'])."</p></div>\n";
}
$id = urlencode($id); //makes browser friendly
//prev. page: **EDIT LINK PATH**
$prev_page = $page - 1;
if($prev_page >= 1) {
echo("<b><<</b> <a href=?page=news&limit=$limit&pagenum=$prev_page><b>Prev.</b></a>");
}
//Display middle pages: **EDIT LINK PATH**
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo("<b> $a</b> | "); //no link
} else {
echo(" <a href=?page=news&limit=$limit&pagenum=$a> $a </a> | ");
}
}
//next page: **EDIT THIS LINK PATH**
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("<a href=?page=news&limit=$limit&pagenum=$next_page><b>Next</b></a> > >");
}
//all done
?>
and here is the code that displays each of the 5 latest news articles
<?
include_once("includes/connection.php");
$q = mysql_query("SELECT id, title FROM news ORDER BY time DESC LIMIT 5");
echo "<OL id=\"blah\">";
while($articles = mysql_fetch_object($q)) {
echo "<LI><a class=\"one\" href=\"http://www.mysite.com/index.php?page=news&id=".$articles->id."\">".$articles->title."</a></LI>";
}
echo"</OL>";
?>
That does display the latest 5 news articles but doesn't take them to the right page