1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php stripping down current URL

Discussion in 'PHP' started by leeds1, Jan 7, 2005.

  1. #1
    I would like to put on my page the directory structure of where the user is:

    so, if the URL was mydomain.com/ subfolder1/ subfolder2 / page-name.html

    they could see that so on the page you would have something like:

    "you are here:" subfolder1 --- subfolder2 --- pagename (without the url)

    with all of these being links.

    OK, the first thing to do is to grab the URL and I can do that as:

    $url = $_SERVER['REQUEST_URI'];

    but how do I split out the URL into the relevant parts

    OH !!! sometimes the subfolder2 is not present, only subfolder 1 so the code would need to allow for whatever is present

    THANKS !
     
    leeds1, Jan 7, 2005 IP
  2. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Let's try explode function
    in your case
    
    $dir_array = explode("/", $url);
    
    PHP:
    You will have to use the count function to know how may element your $dir_array haves
     
    goldensea80, Jan 7, 2005 IP
  3. Ned

    Ned Active Member

    Messages:
    402
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    78
    #3
    
    
    $domain = "http://www.yourdomain.com/";
    
    $url = $PHP_SELF;
    
    $url = explode('/',$url);
    
    $links = array();
    
    foreach($url AS $key->$value)
    {
            $folder = "".$folder."/$value";
            $links[$value] = "/ <a href=\"$domain/$folder/\">$value</a> ";
    };
    
    foreach($links AS $key->$value)
    {
    echo " $value";
    };
    
    
    Code (markup):
    Not actually tested it but presume it works. WIll test later properly.
     
    Ned, Jan 7, 2005 IP
  4. leeds1

    leeds1 Peon

    Messages:
    585
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks for having a go

    but nothing happens - no errors - no characters on the page
     
    leeds1, Jan 7, 2005 IP
  5. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #5
    OK, I'll help you from A, now is Z, just copy and paste:
    
    <?
    //Parse URL version 1
    //(C) 2004 haibuihoang@yahoo.com
    //For example, $url will be
    $url= "http://www.yourdomain.com/sub1/sub2/yourpage.htm";
    $parsed_url=parse_url($url);
    $sub_folder = explode('/',$parsed_url['path']);
    $domain=$parsed_url['scheme'].'://'.$parsed_url['host'];
    $folder="";
    $max=count($sub_folder);
    echo 'You are here: ';
    for ($i=0;$i<$max;$i++)
    {
      $folder.= ($max-1==$i)? $sub_folder[$i] : $sub_folder[$i].'/';
      $folder_url=  $domain.$folder;
      if (0==$i)
      {
        echo "<br><a href=\"$folder_url\">".$domain.' </a>';
      }
      else
      {
        echo "/<a href=\"$folder_url\">".$sub_folder[$i].' </a>';
      }
    
    }
    ?>
    
    PHP:
     
    goldensea80, Jan 8, 2005 IP
  6. leeds1

    leeds1 Peon

    Messages:
    585
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks - that didnt quite work - for others, here is the correct script:

    $url = $_SERVER['REQUEST_URI'];
    $parsed_url=parse_url($url);
    $sub_folder = explode('/',$parsed_url['path']);
    $domain=$parsed_url['scheme'].$parsed_url['host'];
    $folder="";
    $max=count($sub_folder);
    echo 'You are here: ';
    for ($i=0;$i<$max;$i++)
    {
    $folder.= ($max-1==$i)? $sub_folder[$i] : $sub_folder[$i].'/';
    $folder_url= $domain.$folder;
    if (0==$i)
    {
    echo '<br><a href="http://www.domain.co.uk'.$folder_url.'">'.$domain.' </a>';
    }
    else
    {
    echo '/<a href="http://www.domain.co.uk'.$folder_url.'">'.$sub_folder[$i].' </a>';
    }

    }
     
    leeds1, Jan 9, 2005 IP
  7. Lever

    Lever Deep Thought

    Messages:
    1,823
    Likes Received:
    94
    Best Answers:
    0
    Trophy Points:
    145
    #7
    So, this displays the "breadcrumb trail" by folder name - anyone got any pointers on how one might go about susbstituting folder names for "real" names i.e. if there's an exisitng folder with an abbreviated name and I wanted to not display this, and use a full name instead?

    For example... if I had a folder /h2gaads/ and I really want to show "How to get ahead in Advertising" on the screen? Can this be done and what level of difficulty would be involved?
     
    Lever, Jan 13, 2005 IP
  8. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #8
    I guess you must have something like database to store the folder name? Or you store it somewhere? If you use a Database, I think there'd be no problem. The later cases, for example, you may store a file name "folder.info" or "info.php" in each directories. Once you get URL to the directory you want (like my previous example), read it, then display it.
     
    goldensea80, Jan 13, 2005 IP
    Lever likes this.
  9. Lever

    Lever Deep Thought

    Messages:
    1,823
    Likes Received:
    94
    Best Answers:
    0
    Trophy Points:
    145
    #9
    Ah, top tip, goldensea80, thankyou. My database experince is limited so I will investigate the folder.info route until I'm better with dbs. Cheers :)
     
    Lever, Jan 13, 2005 IP
  10. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #10
    You are welcome ;)
     
    goldensea80, Jan 13, 2005 IP
  11. Traeonna

    Traeonna Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I've been looking for something similar. It seems that all the breadcrumb stuff I've found will not work with this current system. I'm sure there's a way around this, but I'm just not that well versed in PHP. See I pretty much design a "template" then call certain information such as content, title, and navigation.

    Placed before <html> on index.php in root (this is my template file).
    
    <?
    $fullPath = '/home/traeonna.com/public_html/ayablue';
    $dir = $_GET['dir'];
    $page = $_GET['page'];
    if(!isset($dir)) {
     $dir = 'home';
     $page = 'index';
     }
    ?>
    
    Code (markup):
    Here are my includes for content, title, and navigation.
    
    <? include($fullPath . '/' . $dir . '/' . $page . '.php'); ?>
    
    <? include($fullPath . '/' . $dir . '/' . $page . '_title.php'); ?>
    
    <? include($fullPath . '/' . $dir . '/' . $dir . '_nav.php'); ?>
    
    Code (markup):
    An example of how the URL looks...
    http://ayablue.traeonna.com/index.php?dir=portfolio_amanda&page=fb_sa

    I would like a breadcrumb as such...
    Home > Amanda's Portfolio > Fruits Basket - Ayame Sohma

    I can print and that will print the directory names and file names, but there's no links, and I kinda would like that.
    
    Home &gt; <?php print $dir . ' > ' . $page; ?> 
    
    Code (markup):
    I'm so frustrated. :mad:
     
    Traeonna, Oct 12, 2005 IP
  12. rosshj

    rosshj Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hey,
    I'm new to PHP but I think some of this code would work with what I'm trying to do. I was wondering if you would know how to display a particular image depending on what section of the website you are in.

    example:

    (something.com/the-band) or (something.com/the-band/something) the photo would be "the-band.jpg" but if you were in (something.com/music) or (something.com/music/something) the photo would be "music.jpg"

    If you or anyone could shed some light, it would be much appreciated.

    Cheers,
    Ross
     
    rosshj, Jan 14, 2006 IP
  13. yo-yo

    yo-yo Well-Known Member

    Messages:
    4,619
    Likes Received:
    205
    Best Answers:
    0
    Trophy Points:
    185
    #13
    You guys made this much more complicated than it should be :)

    For a URL like this:mydomain.com/ subfolder1/ subfolder2 / page-name.html

    
    $uri = $_SERVER['REQUEST_URI'];
    list($trash, $subfolder1, $subfolder2, $pagename) = split("/", $uri);
    PHP:
    Then you can do whatever with each variable
     
    yo-yo, Jan 16, 2006 IP
  14. DavidAusman

    DavidAusman Peon

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You can also use explode( ) function to break it into array pieces
    
    $url = $_SERVER['REQUEST_URI'];
    $array = explode("/",$url);
    echo $array[?]; ///which folder would you want to access ?
    
    PHP:
     
    DavidAusman, Jan 16, 2006 IP