View Full Version : breadcrumbs from path

08-29-2011, 06:04 AM
I created a custom photo gallery script a while back and now I wish to update it to add some breadcrumbs for easier navigation of the gallery. Everything seems to be going well except for linking to the specific galleries.

The gallery is viewed such as:
page URL: gallery.php?album=/Dir1/Dir2/Dir3/Dir4
It's based of the file directory of the primary Gallery Folder. When album is empty, that is the root folder; name "photos" for example.

I currently have the below php snippet I created to create these breadcrumb links.

$path = @$_GET["album"];

if(empty($path)) {
//Echo Nothing as user is in the root directory.
else {
$explode = explode('/',$path);
echo "<pre>";
$links = '';
for($i=0;$i<count($explode);$i++) {
if(empty($explode[$i])) {
$explode[$i] = "<a href='gallery.php' class='breadcrumb'>Main Gallery</a>";
$theLink = '';

for($j=$i;$j<count($explode);$j++) {
if($i==0) {
}else {
$theLink .= '/'.$explode[$j];

$links .= " / <a href='gallery.php?album=".$theLink."' class='breadcrumb'>".$explode[$i]."</a>";

echo $links;


The text portion works fine. It would out put the correct breadcrumbs while in the albums. However, the link paths are... backwards, incorrect. Not sure how to explain it. Any ideas on how to fix this minor glitch. It has been frustrating me for the past few hours.

08-29-2011, 03:48 PM
Seeing as you haven't had an answer yet, I'll jump in :).

I had trouble following your logic, but I think that's mainly my fault - I would have done this completely different. For one thing, you don't need a nested for loop. I'll show you my method:

$path = '/Dir1/Dir2/Dir3/Dir4';

$folders = explode('/',$path);

// Remove the null start value from the array.
// This comes from the 'left' of the first / in your dir

// Set a variable to check for first pass
$i = 0;

// Loop through every folder in your dir
foreach($folders as $key => $folder){
if($i == 0){
// If first pass, append first item with a / at the start
$folders[$key] = '/'.$folder;
echo '<a href="gallery.php">Root Gallery</a>';
// Increment $i to signify first pass is over
// Get previous key's value, and add that to the start of the current key
$folders[$key] = $folders[$key-1].'/'.$folder;
echo '--> <a href="gallery.php?album='.$folders[$key].'">'.$folder.'</a>';

If you want, do a var_dump() of the $folders array after the foreach to see what the structure of it looks like. This seems the most efficient to me. :)

Note: If your path has a trailing /, there will be an empty string at the end of the $folders array. Best practise would probably be to use trim() providing a character set of only /. If you do use trim, remember to remove the unset() because the first key wont be empty anymore :P. If you're wanting a hand with that, let me know

08-30-2011, 03:25 AM
I ended up using your code, thanks. It was getting late and I had been working on it for parts of the day so my mind was all over the place. :)

I had to remove your unset in order for it to display correctly, to explain we have the album path of "/Dir1/Dir2/Dir3/Dir4" (and so on).

Dir 1, isn't the root directory. It's actually the first album in the root directory. After I removed the unset to recognize the "Root" in front of the first slash, it showed up perfectly.

Thanks again!