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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    England
    Posts
    172
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Going back directories

    Is there a way in PHP to work out how many ../../'s the page is from the root directory of the site ?

    For example if I have an included txt file on all my pages, and some pages are at different directory levels, and there is a link like 'blah/blah.php' in the txt file, if someone on a page like mysite.com/xyz/abc/123.php were to click it, it would try and go to mysite.com/xyz/abc/blah/blah.php.

    So I want it to print the correct number of dots and forward slashes before the url, so it would be ../../blah/blah.php on the 123.php page.

    Hope that doesn't sound too complicated
    \o/

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    links from any included file will be relative to the heirarchical top document.

    eg
    /index.php includes folder/folder/a.php which links to /dir/dir/b.php

    that will link to /dir/dir/b.php from where index.php is.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    England
    Posts
    172
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry i'm not good at explaining..

    x/y/x/indexA.php includes global/xyz.txt which has the link 'b/v/c/indexB.php' in it.

    If the user clicks that link on indexA.php then it will try to go to x/y/z/b/v/c/indexB.php, because the ../../../ are not before the url link.

    If it was ../../../b/v/c/indexB.php then it would go to b/v/c/indexB.php.

    I would like to know, how using PHP can it work out how many '../' to put before the url in the link to indexB.php so that it will go straight to it.
    \o/

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    If you're on linux, you can just put the full url in the includes. It won't work on a windows machine though

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    England
    Posts
    172
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nightfire obviously I know I could put the full URL but I don't want to
    \o/

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    at a dodgy guess (ie play around with until this works)

    $num_dirs = count(explode('/',$_SERVER['REQUEST_URI']))-2;

    really you should not need workaround scripts like that though - perhaps taking a few hours to sort out your directory structure would be time better spent.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    England
    Posts
    172
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Okii.

    I have this so far:

    PHP Code:
    <?php
    $num_dirs 
    count(explode('/',$_SERVER['REQUEST_URI']))-2;
    if (
    $num_dirs == "1") { $dots "../"; };
    if (
    $num_dirs == "2") { $dots "../../"; };
    if (
    $num_dirs == "3") { $dots "../../../"; };
    if (
    $num_dirs == "4") { $dots "../../../../"; };
    if (
    $num_dirs == "5") { $dots "../../../../../"; };
    if (
    $num_dirs == "6") { $dots "../../../../../../"; };
    if (
    $num_dirs == "7") { $dots "../../../../../../../"; };
    if (
    $num_dirs == "8") { $dots "../../../../../../../../"; };
    if (
    $num_dirs == "9") { $dots "../../../../../../../../../"; };
    if (
    $num_dirs == "10") { $dots "../../../../../../../../../../"; };
    echo 
    "<a href=\"$dots index.php\">index</a>";
    ?>
    Isn't there is a simpler way of doing that instead of having 10 'ifs' ?

    Also I can't figure out how to get the dots and slashes to join with the index.php in the echo I tried $dotsindex.php but it doesn't work.
    \o/

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This basically does the same ( i think ) but it uses a loop, so you could loop through several times, more or less than 10.

    PHP Code:
    <?php
    $num_dirs 
    count(explode('/',$_SERVER['REQUEST_URI']))-2;
    $dot '../';

    for(
    $i 1$i 11$i++)
    {

        for(
    $x 0$i $i$x++)
        {
            
    $da .= $dot;
        }

        eval(
    'if($num_dirs == "'.$i.'") { $dots = '.$da.'; }');

    }

    echo 
    "<a href=\"$dots index.php\">index</a>";
    ?>
    Hope this helps

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the number of ../'s always reflects $num_dir, so

    $dots = '';
    for($a=0; $a < $num_dirs; $a++)
    {
    $dots.='../';
    }

    would be all you'd need.

    then

    echo '<a href="' .$dots. 'index.php">........';
    Last edited by Ökii; 04-10-2003 at 06:51 PM.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    lol! I cant believe I missed that! That seems so simple now! Thanks Okii!


  •  

    Posting Permissions

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