[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create links for breadcrumb navigation using pathname

Posted on 2007-08-04
5
Medium Priority
?
400 Views
Last Modified: 2010-05-18
Creating crumbs trail/breadcrumb navigation such that for a page with URL domain.com/store/specials/, each of the words in the trail would be a link:
Home > Store > Specials  .. would reference:
domain.com > domain.com/store/ > domain.com/store/specials
0
Comment
Question by:controltheweb
  • 3
5 Comments
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 200 total points
ID: 19632497
<?php

/* Source    Title: PHP Hacks
                   Author: Jack D. Herrington
                   Publisher: O'Reilly
                   ISBN: 0-596-10139-2
             
This notice must stay with the code to be use.

Please read O'Reilly "fair use" policy here

http://www.oreilly.com/pub/a/oreilly/ask_tim/2001/codepolicy.html */


$id = $_GET['id'];
if ( strlen( $id ) < 1 )
  $id = "home";


$pages = array(
  home => array( id=>"home", parent=>"", title=>"Home", url=>"showpage.php?id=home" ),
  users => array( id=>"users", parent=>"home", title=>"Users", url=>"showpage.php?id=users" ),
  jack => array( id=>"jack", parent=>"users", title=>"Jack", url=>"showpage.php?id=jack" )
  );

function breadcrumbs( $id, $pages )
{
  $bcl = array();
  $pageid = $id;
  while( strlen( $pageid ) > 0 )
  {
    $bcl[] = $pageid;
    $pageid = $pages[ $pageid ]['parent'];
  }
  for( $i = count( $bcl ) - 1; $i >= 0; $i-- )
  {
    $page = $pages[$bcl[$i]];
    if ( $i > 0 )
    {
      echo( "<a href=\"" );
      echo( $page['url'] );
      echo( "\">" );
    }
    echo( $page['title'] );
    if ( $i > 0 )
    {
      echo( "</a> | " );
    }
  }
}
?>
<html>
<head>
<title>Page - <?php echo( $id ); ?></title>
</head>
<body>
Breadcrumbs: <?php breadcrumbs( $id, $pages ); ?><br/>
Page name: <?php echo( $id ); ?>
</body>
</html>

To run the script save it as showpage.php upload it to your server, when you open it in your browser all you will have for the breadcrump is Home, now ad to your URL in the address bar ?id=jack and try it again.
0
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19633263
I think there is a better solution than the one proposed above.

$path = explode('/',$_SERVER['SCRIPT_NAME']);
array_pop($path); //last item not needed
echo "<a href='/'>Home</a> ";
$link = '/';
while(!empty($path))
{
      $page = array_shift($path);
      if(strlen($page) > 0)
      {
            $link .= $page . '/';
            echo "&gt; <a href='$link'>$page</a> ";
      }
}
0
 
LVL 13

Accepted Solution

by:
MasonWolf earned 1800 total points
ID: 19633267
I keep forgetting to hit "Proposed Solution" when I submit these.

$path = explode('/',$_SERVER['SCRIPT_NAME']);
array_pop($path); //last item not needed
echo "<a href='/'>Home</a> ";
$link = '/';
while(!empty($path))
{
      $page = array_shift($path);
      if(strlen($page) > 0)
      {
            $link .= $page . '/';
            echo "&gt; <a href='$link'>$page</a> ";
      }
}
0
 
LVL 3

Author Comment

by:controltheweb
ID: 19634128
Looks good. Don't have server access to test it at the moment, but soon!
0
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19634181
It works. Here's a page where I tested it myself before publishing my comment:

http://edocwizard.com/protected/class/language/temp.php
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question