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

I myself order my pictures by title. And added next and prev links. Also, when I get to the last picture, I made some modification so that next will point to the first picture in the album.

What I did is change the template.php and node.tpl.php files in my template folder. My version is similar to that of some earlier posts in this thread, but nevertheless I copy it here for you :

in template.php you need to have the following function :

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 title FROM {node} WHERE nid = $current_nid");
  $current_title = 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.title ";
  switch ($btn_type) {
        case "next":
                $sql .= "> ";
                $sort = "ASC";
                break;
        case "prev":
                $sql .= "< ";
                $sort = "DESC";
                break;
        case "parent":
$query = db_query("SELECT name FROM {term_data} WHERE tid = $tid;");
        $name = db_result($query);
    return l($label.$name, "$type/tid/$tid", array("title" => $name, "class" => $class));
    default:
                return NULL;
                break;
   }
// the trick here is to sort by title (ORDER BY n.title)
  $sql .= "'".$current_title ."' AND r.tid = ". $tid ." AND n.status = 1 ORDER BY n.title $sort;";
  $query = db_query($sql);
  $result = db_fetch_array($query);
if (!$result)
{
  // here I changed so that instead of "back to album" link for first
// and last pictures I link to the last and first pictures respectively
     $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;
      $sql.="' AND r.tid = " . $tid . " AND n.status = 1 ORDER BY n.title $sort;";
  $query = db_query($sql);
  $result = db_fetch_array($query);
return l($label, "node/".$result['nid'], array("title" => $label, "class" => $class));
}
else {
    return l($label, "node/".$result['nid'], array("title" => $label, "class" => $class));
  }
}

in node.tpl.php : it took me quite a while to fix the first and next links to have them look nicer, but the way to do it for your theme might be different. My theme is Slash green :

so, here is my node.tpl.php file :

<!-- start node -->
<div class="node<?php print ($sticky) ? ' sticky' : ''; ?>">
<div class="title1"><div class="title2">
<?php if ($page == 0): ?>
<h1 class="title"><a href="<?php print $node_url ?>"><?php print $title ?></a></h1>
<?php else: ?>
<h1 class="title"><?php print $title ?></h1>
<?php endif; ?>
</div></div>
<?php if ($terms): ?>
<div class="terms">
<?php
 
print $terms ?>
</div>
<?php endif; ?>
<div class="info"><?php print $submitted ?></div>
<?php print $picture ?>
<div class="content">
<?php echo $content; ?>
</div>
  <?php if ($type == 'image'): ?>
   <div class="more">
  <?php
$next
= next_prev($node->nid, 'image', 'next', 'Sekva>>', 'test');
 
$previous = next_prev($node->nid, 'image', 'prev', '<<Malsekva', 'test');
 
$last = next_prev($node->nid, 'image', 'Lasta', '>|', 'test');
 
$first = next_prev($node->nid, 'image', 'Unua', '|<', 'test');
 
$gallery = next_prev($node->nid, 'image', 'parent', '', 'test');
  echo
"<center><b>";
  echo
"(" . $first."&nbsp;&nbsp;";
  if (
$previous){ echo $previous."  ";}
  echo
"&nbsp;&nbsp;[ ".$gallery." ]&nbsp;&nbsp;";
  if (
$next){ echo "  ".$next;}
  echo
"&nbsp;&nbsp;".$last.")</b><br></<center>";
?>

<?php if ($links): ?>
<?php print $links ?>
<?php endif; ?>
</div><?php endif; ?>
<?php if ($links and ($type!='image')): ?>
<div class="more"><?php print $links ?></div>
<?php endif; ?>
</div>
</div>

of course change "sekva", "malsekva", etc by the translations for your language. My site is in Esperanto.

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

Comments

The next_prev function as posted above contains an error which allows for possible SQL code injections because the title arguments aren't escaped. By posting an image with SQL code in the title field, an attacker might gain full access to the database.
Also, it produces SQL errors when trying to resolve the parent gallery if images don't reside inside a gallery.

I've opted to just display the non-clickable text t('no gallery') if there's no parent gallery for the image. The navigation also changes to flip between gallery-less images when browsing outside a gallery.

The following code should correct both issues:

<?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 title FROM {node} WHERE nid = $current_nid");
 
$current_title = db_result($query);
 
$sql = "SELECT n.nid, n.title FROM {node} n ";
  if (
$tid) {
   
/* only join if this image is in a gallery */
   
$sql .= "INNER JOIN {term_node} t ON n.nid = t.nid " .
   
"INNER JOIN {term_data} r ON t.tid = r.tid ";
  }
 
$sql .= "WHERE n.type = '" . $type . "' ";
 
/* if the image is outside galleries, find other images outside galleries
   and provide next/prev navigation between them */
 
if (!$tid)
   
$sql .= "AND n.nid NOT IN (SELECT k.nid FROM {term_node} k)";
 
$sql .= " AND n.title ";
  switch (
$btn_type) {
  case
"next":
   
$sql .= "> ";
   
$sort = "ASC";
    break;
  case
"prev":
   
$sql .= "< ";
   
$sort = "DESC";
    break;
  case
"parent":
    if (!
$tid)
      return(
'(' . t('no gallery') . ')');
   
$query = db_query("SELECT name FROM {term_data} WHERE tid = $tid;");
   
$name = db_result($query);
    return
l($label.$name, "$type/tid/$tid", array("title" => $name, "class" => $class));
  default:
    return
NULL;
    break;
  }
 
// the trick here is to sort by title (ORDER BY n.title)
 
$sql .= "'". db_escape_string($current_title) . "'" .
   
" AND n.status = 1" .
    (
$tid ? " AND r.tid = ". $tid : '') .
   
" ORDER BY n.title $sort;";
 
$query = db_query($sql);
 
$result = db_fetch_array($query);
  if (!
$result && $tid)
    {
     
// here I changed so that instead of "back to album" link for first
      // and last pictures I link to the last and first pictures respectively
     
$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;
     
$sql.="' AND r.tid = " . $tid . " AND n.status = 1 ORDER BY n.title $sort;";
     
$query = db_query($sql);
     
$result = db_fetch_array($query);
      return
l($label, "node/".$result['nid'], array("title" => $result['title'], "class" => $class));
    }
  else {
    return
l($label, "node/".$result['nid'], array("title" => $result['title'], "class" => $class));
  }
}
?>