Breadcrumbs

calendar September 27, 2009

Breadcrumbs is a navigation aid used in user interfaces. It gives users a way to keep track of their location within programs or documents. A breadcrumb trail allows a user to navigate back up the heirarchy where he would find more revelant information.

Code:

<?php
 $id = $_GET['id'];
 if(strlen($id) < 1)
 $id = "profile";
 $pages = array(
                 'profile' => array(
                                    'id' => 'profile',
                                    'previous' => '',
                                    'title' => 'Profile',
                                    'url' => 'mypage.php?id=profile'
                                   ),
                 'bookmarks' => array(
                                      'id' => 'bookmarks',
                                      'previous' => 'profile',
                                      'title' => 'Bookmarks',
                                      'url' => 'mypage.php?id=bookmarks'
                                     ),
                 'comments' => array(
                                     'id' => 'comments',
                                     'previous' => 'comments',
                                     'title' => 'Comments',
                                     'url' => 'mypage.php?id=comments'
                                    )
              );
 //------------------------------------------------------------------------
 function breadcrumbs($id, $pages)
 {
   $n = array();
   while(strlen($id) > 0)
   {
     $n[] = $id;
     $id = $pages[$id]['previous'];
   }
   for($i=count($n)-1; $i>=0; $i--)
   {
     $page = $pages[$n[$i]];
     if($i > 0)
     {
       print "<a href=\"{$page['url']}\">";
     }
     print $page['title'];
     if($i > 0)
     {
       print "</a> | ";
     }
   }
 }
 ?>
 <html>
 <head>
 <title><?php print $id; ?></title>
 </head>
 <body>
 Location: <?php breadcrumbs($id, $pages); ?><br />
 Page: <?php print $id; ?>
 </body>
 </html>

Save this code as mypage.php in your php server.

admin

Leave a Reply

You must be logged in to post a comment.