Last updated August 26, 2009. Created on April 6, 2007.
Edited by jjkd, add1sun. Log in to edit this page.

I only needed next | prev so I simplified the code a bit and added append prepend text support. This goes to template.php. I also had do fix the first SQL query.

  function next_prev($current_nid, $type, $button_type, $label, $prepend_text=NULL, $append_text=NULL) {
   $tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} tn INNER JOIN {node} n ON tn.nid = n.nid WHERE tn.nid = $current_nid")));
   switch ($button_type) {
     case 'next':
       $sort= 'DESC';
       $case = '< ';
       break;
     case 'prev':
       $sort = 'ASC';
       $case = '> ';
       break;
     default:
       return NULL;
       break;
   }
   $sql = "SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid ";
   $sql .= "INNER JOIN {term_data} r ON t.tid = r.tid WHERE n.type = '". $type ."' AND n.nid ". $case;
   $sql .= $current_nid ." AND r.tid = ". $tid ." AND n.status = 1 ORDER BY nid ". $sort;
   $result = db_fetch_array(db_query(db_rewrite_sql($sql)));
   if (!$result) {
     return NULL;
   } else {
     return $prepend_text.l($label, 'node/'. $result['nid'], array('title' => $result['title'])).$append_text;
   }
}

In node.tpl.php I put this

if($page!=0&& $node->type=='image')
    {
        $previous_node_link = next_prev($node->nid, $node->type, 'prev', t('previous'), '<< ', NULL);
        $next_node_link = next_prev($node->nid, $node->type, 'next', t('next'), NULL, ' >>');

        print '';
    }

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

anaramirez’s picture

I’m using DraggableViews module.

I create a view where I show all thumbnails of my gallery. Using DraggableViews my client can sort the pictures as he wants giving a specific order to the thumbnails.
When somebody makes click in a thumbnail, it calls the node related with it.

So… There is a way to call the “Next” and “Previews” node following the value or order the nodes have on the gallery I created with the DraggableViews?

Thanks,
Ana.

rajesh_1988’s picture

I followed all steps.still i am no getting.what is wrong there?
function next_prev($current_nid, $type, $button_type, $label, $prepend_text=NULL, $append_text=NULL) {
$tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} tn INNER JOIN {node} n ON tn.nid = n.nid WHERE tn.nid = $current_nid")));
switch ($button_type) {
case 'next':
$sort= 'DESC';
$case = '< ';
break;
case 'prev':
$sort = 'ASC';
$case = '> ';
break;
default:
return NULL;
break;
}
$sql = "SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid ";
$sql .= "INNER JOIN {term_data} r ON t.tid = r.tid WHERE n.type = '". $type ."' AND n.nid ". $case;
$sql .= $current_nid ." AND r.tid = ". $tid ." AND n.status = 1 ORDER BY nid ". $sort;
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
if (!$result) {
return NULL;
} else {
return $prepend_text.l($label, 'node/'. $result['nid'], array('title' => $result['title'])).$append_text;
}
}

if($page!=0&& $node->type=='image')
    {
        $previous_node_link = next_prev($node->nid, $node->type, 'prev', t('previous'), '<< ', NULL);
        $next_node_link = next_prev($node->nid, $node->type, 'next', t('next'), NULL, ' >>');

        print '';
    }