Previous & Next links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Previous & Next links

Post by kanchan »

i do have news tables having these columns :
n_id
short_news
full_news

Code: Select all

<?
include 'connect.php';
 
$query = mysql_query("SELECT * FROM news where n_id = '$id'");
 
while($row = mysql_fetch_array($query)){ 
$id = $row["n_id"];
$short = $row["short_news"];
$full = $row["full_news"]; 
 
echo $short;
echo "<br>";
echo $full;
echo "<br>";
 
 ?>
Now, what i want is if its the first news then it should display the "Next Article" link and if its in the middle then it should display both "Previous Article" and "Next Article" only...

how can i do that?
can you please post me the code?
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: Previous & Next links

Post by Jaxolotl »

I suggest you do not relay on using the record id to retrieve the record information the way you do if you don't need to.
First of all because the record numbers on auto incremental indexes may not be sequential (for example by deleting a record in between)
Second you should add more security measures if you use a POST/GET to dynamically modify a query, if you can you may use internal pointers like in the example i wrote for you.
oh, it's my opinion, not the TRUTH at all, but just a point of view.

I hope it'll be usefull

Code: Select all

 
<?php
#############################################
###       INCLUDES / REQUIRED
#
  include 'connect.php';
 
#############################################
###       START PAGING
#
 
// limit of results per query 
  settype($limit,"integer");
  $limit = 1;
 
// Count how many records do I have on the db table
  $query = mysql_query("SELECT count(`n_id`) FROM `news`");
  
// fetch the results
  $total_results = mysql_fetch_row($query);
 
// Make them a usable variable
  $total_records = $total_results['0'];
 
////Count needed pages to show all records (just in case you need more than 1 per page)
  $total_pages = ceil($total_records/$limit);
 
////Set offset and limit for query
  if (!isset($_REQUEST['p_offset'])||!is_numeric($_REQUEST['p_offset'])){
    $p_offset = $limit;     //page offset
    $q_offset = 0;              //query offset
  }
  else{
    if(((int)$_REQUEST['p_offset'] < $total_records + $limit)&&((int)$_REQUEST['p_offset'] > $limit)){
      $p_offset = (int)$_REQUEST['p_offset'];                       //page offset
      $q_offset = (int)$_REQUEST['p_offset'] - $limit;      //query offset
    }
  
    else{
      $p_offset = $limit;   //page offset
      $q_offset = 0;    //query offset
    }
  }
 
////Set offset for links
  $offset = $limit;
 
// CREATE A MULTI LINK PAGING HTML OUTPUT IF YOU NEED IT
  if ($total_pages > 1){
    ////////// CREATE MULTI PAGE LINKS
  
    $page_numbers = "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"2\">
    \n<tr>
    <td colspan=\"4\"  align=\"left\">
    <span>";
    for($n_page=1 ; $n_page <= $total_pages ; $n_page++){
      if ($n_page>1){
        $page_numbers .= " | ";
      }
      if(ceil($p_offset/$limit) == $n_page){
        $page_numbers .= "\n<strong>Page ". $n_page."</strong>";
      }
      else{
        $page_numbers .= "\n<a href=\""
        . $_SERVER['PHP_SELF']
        . "?p_offset=".$offset
        . "\">".$n_page
        . "</a>\n";
      }
      $offset = $offset+$limit;
    }
    $page_numbers .= "\n</span>
    \n</td>
    \n</tr>
    \n</table>";
  }
 
// PRINT THE MULTIPAGING HTML
  echo (isset($page_numbers) ? $page_numbers : "");
 
// CREATE A PREVIOUS/NEXT LINK PAGING HTML OUTPUT IF YOU NEED IT
  if ($total_pages > 1){
    $previous_next = "<table width=\"100%\" border=\"1\" cellpadding=\"3\" cellspacing=\"2\">\n<tr>";
    $previous_next .= "\n<td width=\"50%\">";
    $previous_next .= (((($p_offset-$limit) <= $total_records) && ($p_offset > $limit))?("<a href=\"".$_SERVER['PHP_SELF']. "?p_offset=".($p_offset - $limit)."\">Previous</a>"):("&nbsp;"));
    $previous_next .= "\n</td>";
    $previous_next .= "\n<td width=\"50%\">";
    $previous_next .= ((($p_offset >= $limit )&&($p_offset < $total_records ))?("<a href=\"".$_SERVER['PHP_SELF']. "?p_offset=".($p_offset + $limit)."\">Next</a>"):("&nbsp;"));
    $previous_next .= "\n</td>";
    $previous_next .= "\n</tr>\n</table>";
  }
 
// PRINT THE PREVIOUS/NEXT LINK PAGING HTML
  echo (isset($previous_next) ? $previous_next : "");
 
#############################################
###       SHOW RECORD DETAILS IF EXIST
#
  if(isset($total_records) && ($total_records > 0)){
      // RETRIEVE THE RECORDS BY USING Limit and offset for your query
      $query = mysql_query("SELECT
      `n_id`,
      `short_news`,
      `full_news`
      FROM `news`
      ORDER BY `n_id` limit ".$q_offset.", ".$limit."");
    
    while($row = mysql_fetch_assoc($query)){
      // Print the result
      echo $row['short_news']."\n<br>\n".$row['full_news']."\n<br>\n";
    }
  }
  else{
    echo "No records found";
  }
?>
 
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: Previous & Next links

Post by kanchan »

Thanks Jaxolotl with the script you posted...
but i was looking for Previous and Next code

when we display the first record from mysql table then...
i just need that previous and next button according to that news id...
when users click on that buttons then it should display the next record calling from its id
are you getting me?

Please help me out??
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: Previous & Next links

Post by Jaxolotl »

the script i wrote show both (numerical index) and (previous /next link) you may use the one you want and comment the other one, the work independently.
It doesn't relies on the news id, it uses the limit and offset to retrieve a record according to the sequence you have on your storage database.
If you need the IS to be used you may modify the script, if you need some more explanations about how the script works just ask
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: Previous & Next links

Post by kanchan »

Code: Select all

 
<?
$id=mysql_real_escape_string($HTTP_GET_VARS[n_id']);
include 'connect.php';
 
$query = mysql_query("SELECT * FROM news where n_id = '$id'");
 
while($row = mysql_fetch_array($query)){ 
$id = $row["n_id"];
$short = $row["short_news"];
$full = $row["full_news"]; 
 
echo $short;
echo "<br>";
echo $full;
echo "<br>";
}
?>


PREVIOUS NEXT

what i want is when i click on those buttons the link should be like this : news.php?n_id=$_id
can we just view next record using the link news.php?n_id=$_id by clicking on those buttons

i just want that....

can you help me?
Post Reply