Web Analytics Made Easy -
StatCounter breadcrumbs from path - CodingForum

Announcement

Collapse
No announcement yet.

breadcrumbs from path

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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; Aug 29, 2011, 10:50 AM.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      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

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎