TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Breadcrumb Navigation? (http://www.talkphp.com/general/2959-breadcrumb-navigation.html)

Aaron 06-14-2008 10:47 PM

Breadcrumb Navigation?
 
Yeah... It just doesn't feel like this is the right way.

I serialize an array of the home/category/.../page breadcrumbness, and then I do this... It just doesn't seem like the right method. Any help?
PHP Code:

// Develop the breadcrumb Navigation
  
public function getLocation($location) {
  
  
//Unserialize the location array.
  
$location unserialize($location);
  
$return ' ';
  foreach (
$location as $url => $title) {
    if (
$url != 'current') {
      if (
$url == ROOT) {
          
$title '<img src="'.ROOT.'resources/images/house_go.gif" alt="Go to the home page" />';
      }
      
$return .= '<a href="' $url '">' $title '</a> » ';
    }
    else {
      
// Caps insensitive
      
if (strtolower($title) == 'home'){
          return 
'<img src="'.ROOT.'resources/images/house.gif" alt="You are at the home page" />';
      }
      
$return .= $title;
    }
  }
  return 
$return
  } 


Orc 06-15-2008 04:20 PM

Quote:

Originally Posted by Aaron (Post 15670)
Yeah... It just doesn't feel like this is the right way.

I serialize an array of the home/category/.../page breadcrumbness, and then I do this... It just doesn't seem like the right method. Any help?
PHP Code:

// Develop the breadcrumb Navigation
  
public function getLocation($location) {
  
  
//Unserialize the location array.
  
$location unserialize($location);
  
$return ' ';
  foreach (
$location as $url => $title) {
    if (
$url != 'current') {
      if (
$url == ROOT) {
          
$title '<img src="'.ROOT.'resources/images/house_go.gif" alt="Go to the home page" />';
      }
      
$return .= '<a href="' $url '">' $title '</a> » ';
    }
    else {
      
// Caps insensitive
      
if (strtolower($title) == 'home'){
          return 
'<img src="'.ROOT.'resources/images/house.gif" alt="You are at the home page" />';
      }
      
$return .= $title;
    }
  }
  return 
$return
  } 


First off, you haven't declared $location as an array yet, it doesn't even try to validate it as an array, which could be something wrong, plus unserialize only works on strings.

Now, breadcrumbs are just links on a navbar correct?

Aaron 06-15-2008 08:18 PM

I was actually hoping for some totally different method >.< breadcrumb navigation is at the top of this page.

" TalkPHP > Developer Forums > General » Breadcrumb Navigation?"

Orc 06-18-2008 12:39 AM

I forgot what a breadcrumb was, thanks.

But your breadcrumb isn't in the sql, so what are you doing?

delayedinsanity 06-18-2008 01:42 AM

There's a lot of different ways you could go about it. I think he's referring to a method of using the URI to build a breadcrumb, not one based on any database. Off the top of my head,

PHP Code:

function crumby ($szCurPage$szSeperator='&raquo;'$szRoot=NULL) {
    
    
$aPath explode('/'$_SERVER['REQUEST_URI']);

    
$szTrail = (isset($szRoot)) ? 'http://'.$szRoot.'/' 'http://'.$_SERVER['HTTP_HOST'].'/';

    
$szReturn ' <a href="'.$szTrail.'">Home</a> '.$szSeperator;        

    
$intLoop count($aPath)-1;

    for(
$i=1$i $intLoop$i++) {

        
$szTitle ucwords(str_replace('_'' '$aPath[$i]));
        
$szTrail .= $aPath[$i].'/';

        
$szReturn .= ' <a href="'.$szTrail.'">'.$szTitle.'</a> '.$szSeperator;
    }

    
$szReturn .= ' <strong>'.$szCurPage.'</strong>';

    return 
$szReturn;
}

echo 
crumby('You Are Here!'); 

This method wouldn't work well with unreadable URI's, but it's a start.
-m

Aaron 06-21-2008 04:07 AM

Actually, it is from a database that I need to pull the information.

The name of the crumb will be the title column of the page, the problem is dynamic URLs and actually figuring out what pages are above the current one.

Also, delayed, why do you say URI? I wouldn't use a URN, and I doubt 99% of the web development community would use one, so why confuse non-web-savvy people like me by saying URI? :(

delayedinsanity 06-21-2008 04:39 AM

I use them pretty interchangeably, considering a URL is a URI. Just one of those pesky habits.
-m

drewbee 06-22-2008 02:55 PM

Please see the following article:
Storing Hierarchical Data in a Database [PHP & MySQL Tutorials]

I am not sure how you have your database setup, but my guess is you allow subcategories upon subcategories correct? And you link them by 'parent_id', which can cause a huge amount of queries.

The tutorial above can show you how to setup categories properly, above all, get all children of x category and its breadcrumbs with a grand total of 3 queries.

This method you dont have to have the sql 'follow' the tree and have to re-query each time it gets a child deeper.

They even have a function on the tutorial somewhere that will convert your parent_id linkage to the correct method.

Good luck!!


All times are GMT. The time now is 12:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0