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

When you display all the images in a gallery they are showed sorted by date, the newest is first. This is also how the prev/next buttons should take you.

In most cases, for new galleries, sorting by date or nid probably will give the same result since you get increasing dates and increasing node ids as you keep adding images.

If you edit an image and if you change the date (Authored on) the you will start running into problems. Here is the code I used to properly navigate by date (and this code also fixes the DESC/ASC sort order):

<?php
function next_prev($current_nid, $type, $btn_type, $label, $class) {
  
$query = db_query("SELECT tid FROM {term_node} WHERE nid = $current_nid;");
  
$tid = db_result($query);
  
$query = db_query("SELECT created FROM {node} WHERE nid = $current_nid");
  
$current_created = db_result($query);
  
$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.created ";
   switch (
$btn_type) {
        case
"next":
               
$sql .= "< ";
               
$sort = "DESC";
                break;
        case
"prev":
               
$sql .= "> ";
               
$sort = "ASC";
                break;
        default:
                return
NULL;
                break;
   }
  
$sql .= $current_created ." AND r.tid = ". $tid ." AND n.status = 1 ORDER BY n.created $sort;";
  
$query = db_query($sql);
  
$result = db_fetch_array($query);
   if (!
$result) {
       
$query = db_query("SELECT name FROM {term_data} WHERE tid = $tid;");
       
$name = db_result($query);
     return
l("Back To $name", "$type/tid/$tid", array("title" => $name, "class" => $class));
   } else {
     return
l($label, "node/".$result['nid'], array("title" => $label, "class" => $class));
   }
}
?>

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

Comments

I'm trying to modify the code above to work with another table (draggableviews_structure) generated by "DraggableTable".

The table "draggableviews_structure" has:
view_name
nid
delta
args
value

Instead of sort by date I would like to sort by "value".

The code I have:

function next_prev($current_nid, $type, $btn_type, $label, $class) {
   $query = db_query("SELECT tid FROM {term_node} WHERE nid = $current_nid;");
   $tid = db_result($query);
   $query = db_query("SELECT value FROM {draggableviews_structure} WHERE nid = $current_nid");
   $current_value = db_result($query);
   $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 INNER JOIN {draggableviews_structure} ds ON n.nid = ds.nid WHERE n.type = '".$type."' AND ds.value ";
   switch ($btn_type) {
        case "next":
                $sql .= "< ";
                $sort = "DESC";
                break;
        case "prev":
                $sql .= "> ";
                $sort = "ASC";
                break;
        default:
                return NULL;
                break;
   }
   $sql .= $current_value ." AND r.tid = ". $tid ." AND n.status = 1 ORDER BY value $sort;";
   $query = db_query($sql);
   $result = db_fetch_array($query);
   if (!$result) {
        $query = db_query("SELECT name FROM {term_data} WHERE tid = $tid;");
        $name = db_result($query);
     return l("Back To $name", "$type/tid/$tid", array("title" => $name, "class" => $class));
   } else {
     return l($label, "node/".$result['nid'], array("title" => $label, "class" => $class));
   }
}

It's not working...
What I'm doing wrong?
Can somebody help me?