• Home
  • About
  • Archives
  • Icon Gallery
Subscribe: Posts | Comments | E-mail
  • Cool Tools
  • Downloads
  • Featured
  • Misc
  • Music
  • PHP/mySQL
  • Video
  • Web Development

Snipe.Net

Posted on June 19, 2002 - by snipe

PHP/mySQL Breadcrumb Trail

Featured PHP/mySQL
PHP/mySQL Breadcrumb Trail
This lets you automatically create a breadcrumb trail navigation through PHP and mySQL, that would look something like:
Home >> Parrots >> Red Parrots
This type of dynamic breadcrumb trail is only possible when you are storing the menu items (or categories) in a table, that contains at least a name, a category id number and a parent category id number.

<?php
/* ———————————————- */
/* ———— BEGIN PHP SNIPPET —————-*/
/* ———————————————- */
// $this_cat_id: the current category id number
// $flarn: just a counter, call it as 0 in your
// function call and forget about it
// $keep_cat_id: the cat id number again - so that
// it can decide whether to make a
// category a link at the top while you’re in the
// “product” page

function get_crumbs($this_cat_id, $flarn, $keep_cat_id) {

$link_to_page=$_SERVER['PHP_SELF'];
if (!isset(
$this_cat_id)) {
// if we are already “home”, dont make home a link
$this_cat_id =“0″;
echo
“Home << “;
}
// get the info and parent id for whatever category
// we’re currently in

$sql = “SELECT id, parent_id, name from categories “;
$sql .=“where id = $this_cat_id”;

$show_crumb_trail = mysql_query($sql);
$num_crumbs = mysql_num_rows($show_crumb_trail);

// if we actually have some results….
if ($num_crumbs > 0) {
list(
$cat_id, $cat_parent, $cat_name) = mysql_fetch_row($show_crumb_trail);
$cat_id_array[$flarn] = $cat_id;
$cat_parent_id_array[$flarn] = $cat_parent;
$cat_name_array[$flarn] = $cat_name;
if (
$cat_id_array[$flarn] > 0) {
mysql_free_result($show_crumb_trail);
// increment $next by one
$next = $flarn+1;
if (
$flarn == 0 ) {
echo
“Home << “;
}
// now lets call the function again to loop through
// the other categories
// until we’re left with none
get_crumbs($cat_parent_id_array[$flarn], $next, $keep_cat_id);

// Since $keep_cat_id is the id number of original
// category we’re in,
// now we check to see whether or not we have to
// make the real category
// name a link or not
// (If we’re looking at the main category display,
// we wouldn’t have to,
// since we’re already *in* the category.  This is
// more useful for when you have a product
// display page, that way the link back to the
// category that item or  product lives in
// will be created
if ($keep_cat_id==$cat_id_array[$flarn]) {
echo
$cat_name_array[$flarn];
} else {
echo
“$cat_name_array[$flarn] << “;
}
}
}
}
?>

You would call the function using something like this:

<php get_crumbs($_REQUEST['cat_id'], “0″, $_REQUEST['cat_id']; ?>

This entry was posted on Wednesday, June 19th, 2002 at 10:48 am and is filed under Featured, PHP/mySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

0 Comments

We'd love to hear yours!



Leave a Comment

Here's your chance to speak.

You must be logged in to post a comment.

  • What I'm Doing...

    • Dear Dell: Fuck you. That is all. 9 hrs ago
    • wtf... now my ipod isn't recognized by my computer? 10 hrs ago
    • is stuck next to Ethyl McCheddarSnatch on the bus again. *sigh* Is personal hygiene really so hard? 13 hrs ago
    • More updates...
  • Tag Cloud

    • blogging breadcrumbs browsers contact management Downloads e-cards ff3 firefox forms free freeware fun geek Geek Humor Graphics Humor image manipulation jonathan coulton lifehacks linking mac mc frontalot Music mysql nedrcore nerdcore nsfw php plaxo satire security seo snippets social networks syncing twitter ui vector Video warcraft webdev windows work WTF zork
  • Make With the Clicky!

  • Categories

    • Cool Tools
    • Downloads
    • Featured
    • Mac Downloads
    • Misc
    • Music
    • PHP/mySQL
    • Video
    • Web Development
    • Windows Downloads
  • Flickr Photos

  • AJAX/Web 2.0

    • AJAXDaddy
    • Noupe
  • CSS

    • Blueprint CSS
    • Noupe
  • Geek Humor

    • Bash.Org
    • Daily WTF
    • Diesel Sweeties
    • FailBlog
    • Penny Arcade
    • xkcd
  • Graphics

    • Adobe Kuler
    • Iconspedia
    • Photoshop Express
    • Smashing Magazine
  • Life Tools

    • LifeHacker
  • Misc

    • 419 Eater
    • Cellphone PSA Cards
    • Glarkware
    • What’s That Bug?
  • Music

    • Hipster, Please!
    • Jonathan Coulton
    • MC Frontalot
    • MC Lars
    • Optimus Rhyme
  • PHP/mySQL

    • PHPBuilder
    • Zend
  • Recent Posts

    • Managing registration spam in vbulletin
    • Turn any photo into a vintage photo
    • Generate lists of banned words for forums and other applications
    • Poll Everywhere Gathers Votes by SMS
    • Track Your Stolen Laptop (for Free) with Adeona
© 2008 Snipe.Net - Bitterness never tasted so sweet
The Papercut theme by WooThemes - Premium Wordpress Themes