[support] Next and last menu item

Clarke Robin Robin.Clarke at networkrail.co.uk
Fri Mar 20 10:53:35 UTC 2009


This is probably not the most efficient way to do this, but maybe this
will give some ideas. I did this in a custom Drupal 5 theme. This is
slightly different to your requirement, in that there is no "Next" link
shown if we are the last sibling. (And same for the "Previous" link.)

robin


========================================================================
=======
In node-<your_type>.tpl.php
========================================================================
=======
<?php print get_prev_link($node->path); ?>

or

<?php print get_next_link($node->path); ?>



========================================================================
=======
In template.php
========================================================================
=======
/**
 * Returns an array of the paths of all the nodes with the same parent
menu item
 * as the current_path.
 */
function _get_sibling_paths($current_path) {

  $paths = array();

  // Find the Parent ID (pid) of this menu item.
  $sql = "select m.pid from {menu} m, {url_alias} u where m.path = u.src
and u.dst = '" . $current_path . "'";
  $menu_pid = db_result(db_query($sql)); 

  // Use the pid to find all paths
  $sql = "select m.mid, m.pid, m.path, u.dst from {menu} m, {url_alias}
u where m.path = u.src and m.pid = " . $menu_pid . " order by m.weight";
  $result = db_query(db_rewrite_sql($sql));
  while ($data = db_fetch_object($result)) {
    $paths[] = $data->dst;
  }

  return $paths;
}

function get_next_link($current_path) {

  $next_path;
  $paths = _get_sibling_paths($current_path);
  foreach ($paths as $path) {
    if ($found) {
      $next_path = $path;
      break;
    }
    if (strcmp($path,$current_path) == 0) {
      $found = true;
    }
  }
  if (isset($next_path)) {
    return l('<img alt="Next" title="Next" src="/files/right.png"
height="36" width="36" />', $next_path, null, null, null, null, true);
  }
  return "";
}

function get_prev_link($current_path) {

  $prev_path;
  $paths = _get_sibling_paths($current_path);
  foreach ($paths as $path) {
    if (strcmp($path,$current_path) == 0) {
      break;
    }
    $prev_path = $path;
  }
  if (isset($prev_path)) {
    return l('<img alt="Previous" title="Previous" src="/files/left.png"
height="36" width="36" />', $prev_path, null, null, null, null, true);
  }
  return '<img alt="Previous" title="Previous" src="/files/left.png"
height="36" width="36" style="visibility: hidden;" />'; 
}
========================================================================
=======

 

-----Original Message-----
From: support-bounces at drupal.org [mailto:support-bounces at drupal.org] On
Behalf Of John Fletcher
Sent: 20 March 2009 08:26
To: support at drupal.org
Subject: [support] Next and last menu item

I've got a standard navigation-type menu with my pages in it, and a
requirement to show a kind of "back and forward arrow" on each page in
Drupal 6. The back arrow takes you to the previous page in the menu and
the forward arrow takes you to the next page in the menu, and the title
of the relevant page is printed next to the arrows. Like:
Home <-  |  -> About us

The "previous page" means the previous sibling menu item or if none,
parent item.
The "next page" mean the child menu item or, if none, next sibling item,
or if none, next sibling of parent item.

I had a look around the menu API but can't put my finger on the most
efficient way to do this. Anyone know?

Regards,
Fletch

--
[ Drupal support list | http://lists.drupal.org/ ]
**************************************************************************************************************************************************************** 

The content of this email (and any attachment) is confidential. It may also be legally privileged or otherwise protected from disclosure. 

This email should not be used by anyone who is not an original intended recipient, nor may it be copied or disclosed to anyone who is not an original intended recipient. 

If you have received this email by mistake please notify us by emailing the sender, and then delete the email and any copies from your system. 

Liability cannot be accepted for statements made which are clearly the senders own and not made on behalf of Network Rail. 

Network Rail Infrastructure Limited registered in England and Wales No. 2904587, registered office Kings Place, 90 York Way London N1 9AG 

**************************************************************************************************************************************************************** 



More information about the support mailing list