Jump to content

Archived

This topic is now archived and is closed to further replies.

SharkBait

Bread Crumbs...HowTo

Recommended Posts

How does one do the breadcrumbs in php?

Do I use a session to keep track of where they are?

Kind like phpfreaks: PHP Help Forums > PHP and MySQL > PHP Help

Share this post


Link to post
Share on other sites
I've been wondering this myself, and yeah, sessions were the first thing that popped into my mind.

Maybe you could make a breadcrum variable that is the link, then add or subtract from it. I dunno.

Share this post


Link to post
Share on other sites
The $_SERVER['PHP_SELF'] variable gives you your url location. Can't you use that to keep track of where the user is?

Share this post


Link to post
Share on other sites
Its not so much where the user is, but where the user came from. For instance, in the breadcrumb for this forum, how does the "PHP Help" link know to be there, and for that matter, the links ahead of it?

Share this post


Link to post
Share on other sites
[a href=\"http://www.zend.com/zend/spotlight/breadcrumb28.php\" target=\"_blank\"]http://www.zend.com/zend/spotlight/breadcrumb28.php[/a]

[a href=\"http://www.baskettcase.com/classes/breadcrumb/\" target=\"_blank\"]http://www.baskettcase.com/classes/breadcrumb/[/a]

have fun

Share this post


Link to post
Share on other sites
breadcrumb examples i've seen use your dir structure. so like, if you store your files in the a dir structure in the same way you want the breadcrumbs to show, you can base your breadcrumb off that

Share this post


Link to post
Share on other sites
[a href=\"http://www.sillybean.net/archives/452\" target=\"_blank\"]http://www.sillybean.net/archives/452[/a] as a follow-up to Crayon Violent's suggestion

Share this post


Link to post
Share on other sites
Ah thanx for the replies. I guess I'll have to do a combination of it.

directory and file ;) I'll play around and see what I can come up with with help of the examples you guys have posted.

Share this post


Link to post
Share on other sites
[!--quoteo(post=388522:date=Jun 27 2006, 07:45 AM:name=SharkBait)--][div class=\'quotetop\']QUOTE(SharkBait @ Jun 27 2006, 07:45 AM) [snapback]388522[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Ah thanx for the replies. I guess I'll have to do a combination of it.

directory and file ;) I'll play around and see what I can come up with with help of the examples you guys have posted.
[/quote]

This is what I came up with:

(how I wish php tags were working ;))
[code]
<ul id="breadcrumbs">
<?php
// Get Directory Path and Scriptname to create BreadCrumbs
$parts = explode("/", dirname($_SERVER['PHP_SELF']));
echo "<li><a href=\"/\"Home</a> ";
foreach($parts as $key => $dir) {
  $label = ucwords($dir);
  $url = "";
  for($i = 1; $i <= $key; $i++) {
    $url .= $parts[$i]."/";
  }
  if($dir != "") {
    echo "<li>> a href=\"/{$url}\">{$label}</a></li> ";
  }
}
$script = explode(".", basename($_SERVER['PHP_SELF']));
if(script != "") {
  echo "<li>> ". ucwords($script[0])."</li>";
}
?>
</ul>
[/code]

Thanx to the sillybean.net example and suggestions :) My Breadcrumbs look like:

Home > Admin > ScriptName

Share this post


Link to post
Share on other sites
Hi all,

I'm also using the Sillybean example. It's quite easy to use but I still have a question. What if I need to remove, or not display, the two first directories. For example, I would like to remove Home and Dir1 from:

Home > Dir1 > Dir2 > Dir3 > Here

Removing Home is easy, just commenting out, but how about Dir1? If I use unset ($parts[1]), it removes Dir1 from the breadcrumb but then the links are wrong.

Thanks
Matt

Share this post


Link to post
Share on other sites
it's hard to determine how to fix that without you showing your code, but if it's because of some kind of loop you are doing that is dependant on the array keys being 12345 instead of 1245 then you can use [url=http://www.php.net/array_values]array_values()[/url] to reset the keys after you have unset one of them.  consider this example:

[code]
<?php
  // example array
  $blah = array('a','b','c','d','e');
 
  // display what the key - value initially looks like
  // will show keys 0,1,2,3,4
  foreach ($blah as $key => $val) {
      echo "$key - $val <br>";
  }
 
  // unset the middle element
  unset ($blah[2]);

  // show what the key-val looks like now
  // shows 0,1,3,4
  foreach ($blah as $key => $val) {
      echo "$key - $val <br>";
  }

  // reset the keys
  $blah = array_values($blah);

  // display results from reseting the keys
  // shows 0,1,2,3 
  foreach ($blah as $key => $val) {
      echo "$key - $val <br>";
    }
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.