• Home
  • About
  • Archives
  • Icon Gallery
Subscribe: Posts | Comments | E-mail
  • 'Net Culture
  • Downloads
  • Music
  • PHP/mySQL
  • Teh Funneh
  • Tools
  • Video
  • Web Dev

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']; ?>

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • E-mail this story to a friend!
  • Furl
  • Ma.gnolia
  • Pownce
  • Reddit
  • Technorati
  • TwitThis

Related Posts

  • Creating a Multi-Level Listbox in PHP/mySQL...
  • Alternating Row Colors in PHP/mySQL...
  • Dynamic thumbnailing with PHP and Imagemagick...
  • Google Style Page Numbering (with x per page and y page numbers displayed)...

  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    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.

    1. Name (required)

      Mail (required)

      Website

      Message

    Subscribe without commenting

    • Categories

    • What I'm Doing...

      • was stoked about her progress until the US armory server locked her out again. http://apps.facebook.com/wow_toons/ 2 hrs ago
      • is still unsure how she feels about people telling her to increase her personal brand. Marketing people... sheesh... 2 hrs ago
      • is making the WoW Armory and Facebook her bitch while on the bus. The powah! 5 hrs ago
      • More updates...
    • Random Thing You Probably Didn't Know About Me

      • I know how to use power-tools, but I throw like a girl
    • Make With the Clicky!

    • 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
      • TehAwesome
      • What’s That Bug?
    • Music

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

      • PHPBuilder
      • Zend
    © 2008 Snipe.Net - Bitterness never tasted so sweet
    The Papercut theme by WooThemes - Premium Wordpress Themes