Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 10 Times in 10 Posts

    Question breadcrumbs from path

    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.
    PHP Code:
    $path = @$_GET["album"];
        
        if(empty(
    $path)) {
            
    //Echo Nothing as user is in the root directory.
        
    }
        else {
            
    $explode explode('/',$path);
            echo 
    "<pre>";
            
    print_r($explode);    
            
    $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.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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:
    PHP Code:
    $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
    unset($folders[0]);

    // 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
            
    $i++;
        }
        else{
            
    // 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
    Last edited by BluePanther; 08-29-2011 at 03:50 PM.

  • Users who have thanked BluePanther for this post:

    dniwebdesign (08-30-2011)

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 10 Times in 10 Posts
    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!
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •