Jump to content


Photo

How to link to a page within pagination


  • Please log in to reply
3 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 12 August 2006 - 12:54 AM

I have a pagination script which displays 1 news article per page and it works exactly how i want it to

I also hae links to the 5 newest articles so a viewer can click on the link and go straight to that page.

The problem is that all the links go straight to the first page.  i.e. if the user clicks on the third link they should be taken to the third newest post but instead they get directed to the newest post.

Is there a way I can get around this?

Pagination code:
<? 
//REMEMBER TO CONNECT TO DATABASE! 

include_once("includes/connection.php");

$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&amp;pagenum=1></a> 
<a href=?page=news&limit=25&amp;pagenum=1></a> 
<a href=?page=news&limit=50&amp;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>&lt;&lt;</b> <a href=?page=news&limit=$limit&amp;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&amp;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&amp;pagenum=$next_page><b>Next</b></a> &gt; &gt;"); 
} 

//all done 
?>

Here is how i display the 5 newest 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>";
?>
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 raza.shahzad

raza.shahzad
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 12 August 2006 - 02:19 AM

are  $articles->id and $articles->title giving correct display?
echo $articles->*; them for test purpose


#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 12 August 2006 - 10:41 AM

yes they are....just when i click each of them they direct to the first page of the pagaination
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 12 August 2006 - 02:31 PM

I have made some changes to how the article are listed and it sort of works
include_once("includes/connection.php");

$q = mysql_query("SELECT id, title FROM news ORDER BY id 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&limit=1&pagenum=".$articles->id."\">".$articles->title."</a></LI>";
}
echo"</OL>";
?>

Now what it does is limit it to one article per page which is right but when you click on the first link which should be the last article insterted it goes to the first article insterted in the pagination whcih would be the last page.  I am trying to get it to go to the first page.  First link should go to the first page, second link to the second page and so on

How do I do this?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users