SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question One path, but hide link per page is different

    I have a menu like this, its in header.php (I else footer.php):

    PHP Code:
    <div id="menu">
       <
    ul>                                                                       
           <
    li class="selected hover"><a href="user/welcome">Home</a></li>
           <
    li class="hover"><a href="user/about">About</a></li>
           <
    li class="hover"><a href="user/specials">Specials</a></li>
           <
    li class="hover"><a href="user/account">Account</a></li>
           <
    li class="hover"><a href="user/register">Register</a></li>
           <
    li class="hover"><a href="user/price">Price</a></li>
           <
    li class="hover"><a href="user/contact">Contact</a></li>
       </
    ul>
    </
    div> <!-- end .menu --> 
    In the home page (index.php), I include header.php and footer.php into one, and menu above works normal. But, when I click About link or other link (sure header.php and footer.php is included), in the about page, hover About, Specials ... I receive some hide link (or url) like http://localhost/demo/foods/user/user/about, http://localhost/demo/foods/user/user/specials. I don't want to them, I want http://localhost/demo/foods/user/about ..., css and jquery are not working too. Have anyone know why? Thank you!

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,177
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    It would be better for your links to have a root relative URL so that the links don't break, because they are relative to the current page location at the moment, so if you hover one of those links on a page that is already in the /uers/ directory, you'll get the URLs you posted above, which is wrong. Something like this would be better:

    <a href="/demo/foods/user/about">About</a>

    But that will only work if you have a local server environment set up. It will also break things when you put this code online. So when building locally, it's better to set up your local environment to reflect the online environment by modifying your hosts file.

    It's also ver inefficient to have the "hover" class on each LI like that. It would be better to target those LIs with #menu li instead, and not use the hover class at all.

  3. #3
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    It would be better for your links to have a root relative URL so that the links don't break, because they are relative to the current page location at the moment, so if you hover one of those links on a page that is already in the /uers/ directory, you'll get the URLs you posted above, which is wrong. Something like this would be better:

    <a href="/demo/foods/user/about">About</a>

    But that will only work if you have a local server environment set up. It will also break things when you put this code online. So when building locally, it's better to set up your local environment to reflect the online environment by modifying your hosts file.

    It's also ver inefficient to have the "hover" class on each LI like that. It would be better to target those LIs with #menu li instead, and not use the hover class at all.
    Can you resolve that problem with CodeIgniter? I see the same problem with CI.

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,177
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    I would presume so, but I don't know it well enough. You might need to give us some more information about your setup.

  5. #5
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    I would presume so, but I don't know it well enough. You might need to give us some more information about your setup.
    I give some information about project without CI, here is my .htaccess:
    PHP Code:
    RewriteEngine On

    RewriteCond 
    %{REQUEST_FILENAME} !-d
    RewriteCond 
    %{REQUEST_FILENAME} !-f
    RewriteCond 
    %{REQUEST_FILENAME} !-l

    RewriteRule 
    ^(.+)$ index.php?url=$[QSA,L]

    ErrorDocument 404 /view/shared/error/404.php 
    index.php
    PHP Code:
    <?php
        
    require_once 'core/loader.php';
        require_once 
    'core/controller.php';
        
        
    $loader = new loader($_GET['url']);
        
    $controller controller::getInstance($loader->controller$loader->action);
        
        include 
    "view/user/include/header.php";
        
    $controller->request();
        
    $controller->render();
        include 
    "view/user/include/footer.php";
    ?>
    url's form: localhost/dpfoods/controller_name/method_name?id=
    I take controller_name, method_name by explode function and pass them to loader class. The loader class will select controller and then controller will select function.

    in index.php (default page, menu above displays normal):
    // main menu
    Home >> About >> ...
    Home - localhost/dpfoods/user/welcome
    About - localhost/dpfoods/user/about

    but in other page, apple details page example:
    // main menu
    Home >> About >> ...
    Home - localhost/dpfoods/user/user/welcome
    About - localhost/dpfoods/user/user/about

    // other menu
    Home >> Fruits and vegetables >> apple
    if true:
    Home - localhost/dpfoods/user/welcome
    Fruits and vegetables - localhost/dpfoods/user/category?id-category=4
    apple - localhost/dpfoods/user/details?id-product=25
    // but all hide links in this menu are the same
    localhost/dpfoods/user/details?id-product=25
    Structure folder:
    28701044.png

  6. #6
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is a problem that has been bugging me for years and I solved it site by site. Now I've come up with a generic solution which works on my LAMP server as well as on OSX. I would be interested to know if it works in other environments.
    PHP Code:
    <?php

    function offset2root() {

        
    $phpSelf explode('/'$_SERVER['PHP_SELF']);
        
    $scriptFilename explode('/'$_SERVER['SCRIPT_FILENAME']);
        
    $root array_intersect($phpSelf$scriptFilename);
        
    $count count($root);
        
    $offset '';
        while (
    $count-- > 2) {
            
    $offset .= '../';
        }
        if(empty(
    $offset)) {
            return 
    './';
        } else {
            return 
    $offset;
        }

    }

    $offset offset2root();

    echo 
    "<a href='{$offset}path/to/file.php'>file.php</a>";

    ?>
    Denny Schlesinger
    web services

  7. #7
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    This is a problem that has been bugging me for years and I solved it site by site. Now I've come up with a generic solution which works on my LAMP server as well as on OSX. I would be interested to know if it works in other environments.
    PHP Code:
    <?php

    function offset2root() {

        
    $phpSelf explode('/'$_SERVER['PHP_SELF']);
        
    $scriptFilename explode('/'$_SERVER['SCRIPT_FILENAME']);
        
    $root array_intersect($phpSelf$scriptFilename);
        
    $count count($root);
        
    $offset '';
        while (
    $count-- > 2) {
            
    $offset .= '../';
        }
        if(empty(
    $offset)) {
            return 
    './';
        } else {
            return 
    $offset;
        }

    }

    $offset offset2root();

    echo 
    "<a href='{$offset}path/to/file.php'>file.php</a>";

    ?>
    Thanks so much! I'll try it and post the result later.

  8. #8
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My first try didn't work as expected. I think this one does.

    PHP Code:
    <?php

    ################################################################
    #                                                              #
    #  depending on how you organize your files you might have to  #
    #  adjust the offsets in                                       #
    #  $count = count($scriptFilename) - $foundOSX - 3;            #
    #  and in                                                      #
    #  $count = count($scriptFilename) - $foundLAMP - 2;           #
    #                                                              #
    ################################################################

    function offset2root() {

        
    $scriptFilename explode('/'$_SERVER['SCRIPT_FILENAME']);
        
    $foundOSX  array_search('Sites'$scriptFilename);
        
    $foundLAMP array_search('public_html'$scriptFilename);
        if(
    $foundOSX  !== FALSE) {
            
    $count count($scriptFilename) - $foundOSX 3;
        } elseif (
    $foundLAMP !== FALSE) {
            
    $count count($scriptFilename) - $foundLAMP 2;
        } else { 
            return 
    './';
        }

        
    $offset '';
        while (
    $count-- > 0) {
            
    $offset .= '../';
        }
        if(empty(
    $offset)) {
            return 
    './';
        } else {
            return 
    $offset;
        }

    }

    $link offset2root();

    echo 
    "<a href='{$link}path/to/file.php'>Menu Item</a>";

    ?>
    Denny Schlesinger
    web services

  9. #9
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    My first try didn't work as expected. I think this one does.

    PHP Code:
    <?php

    ################################################################
    #                                                              #
    #  depending on how you organize your files you might have to  #
    #  adjust the offsets in                                       #
    #  $count = count($scriptFilename) - $foundOSX - 3;            #
    #  and in                                                      #
    #  $count = count($scriptFilename) - $foundLAMP - 2;           #
    #                                                              #
    ################################################################

    function offset2root() {

        
    $scriptFilename explode('/'$_SERVER['SCRIPT_FILENAME']);
        
    $foundOSX  array_search('Sites'$scriptFilename);
        
    $foundLAMP array_search('public_html'$scriptFilename);
        if(
    $foundOSX  !== FALSE) {
            
    $count count($scriptFilename) - $foundOSX 3;
        } elseif (
    $foundLAMP !== FALSE) {
            
    $count count($scriptFilename) - $foundLAMP 2;
        } else { 
            return 
    './';
        }

        
    $offset '';
        while (
    $count-- > 0) {
            
    $offset .= '../';
        }
        if(empty(
    $offset)) {
            return 
    './';
        } else {
            return 
    $offset;
        }

    }

    $link offset2root();

    echo 
    "<a href='{$link}path/to/file.php'>Menu Item</a>";

    ?>
    Could you tell me how does your code work? At first glance, looks your code, it seems being used to include file. Right now, I knew my problem and I should make absolute link.

  10. #10
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greedyman View Post
    Could you tell me how does your code work? At first glance, looks your code, it seems being used to include file. Right now, I knew my problem and I should make absolute link.
    Sure! The code calculates the offset between the domain root and the file calling the function.

    On a LAMP server the domain html base is at "public_html":
    Code:
    /home/user/public_html/some_folder/another_folder/script.php
    On an OSX Mac the domain html base is at "Sites":
    Code:
    /127.0.0.1/_user/Sites/some_folder/another_folder/script.php
    First the function finds the domain html base: "public_html" or "Sites"
    PHP Code:
    $scriptFilename explode('/'$_SERVER['SCRIPT_FILENAME']);
    $foundOSX  array_search('Sites'$scriptFilename);
    $foundLAMP array_search('public_html'$scriptFilename); 
    Next the function calculate the offset between the domain html base and calling script (plus or minus an adjustmet as needed). Either
    Code:
    $foundOSX or $foundLAMP
    has the location of the domain html base while
    Code:
    count($scriptFilename)
    has the location of the calling script.

    If a base is not found the function returns './' (no offset):
    PHP Code:
    if($foundOSX  !== FALSE) {
        
    $count count($scriptFilename) - $foundOSX 3;
    } elseif (
    $foundLAMP !== FALSE) {
        
    $count count($scriptFilename) - $foundLAMP 2;
    } else { 
        return 
    './';

    If a base is found the function returns the proper number of '../' to reach the domain html base:
    PHP Code:
    $offset '';
    while (
    $count-- > 0) {
        
    $offset .= '../';
    }
    if(empty(
    $offset)) {
        return 
    './';
    } else {
        return 
    $offset;

    To see it more graphically add the following to the top of the function. It will show the array the function works with:
    PHP Code:
    echo "\$_SERVER['SCRIPT_FILENAME']= {$_SERVER['SCRIPT_FILENAME']}<br>\n";
    $scriptFilename explode('/'$_SERVER['SCRIPT_FILENAME']);
    echo 
    "scriptFilename= \n<pre>\n";
    print_r ($scriptFilename);
    echo 
    "</pre>\n"
    The reason I use the extra offset on the Mac is because I work on multiple domains
    Code:
    /127.0.0.1/_user/Sites/domain-1.com/
    /127.0.0.1/_user/Sites/domain-2.com/
    /127.0.0.1/_user/Sites/domain-3.com/
    making each domain's base one deeper than "Sites'
    Denny Schlesinger
    web services

  11. #11
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @captainccs I work on XAMPP server as well as on Windows 8. Look again my index.php:
    PHP Code:
    <?php 
        
    require_once 'core/loader.php'
        require_once 
    'core/controller.php'
         
        
    $loader = new loader($_GET['url']); 
        
    $controller controller::getInstance($loader->controller$loader->action); 
         
        include 
    "view/user/include/header.php"
        
    $controller->request(); 
        
    $controller->render(); 
        include 
    "view/user/include/footer.php"
    ?>
    and my url form: localhost/dpfoods/controller_name/method_name?id=. The fisrt thing, header and footer is always putted on index file (see my structure folder), if I remove controller_name in url, it's mean url's form: localhost/dpfoods/method_name?id= and have no problem. Contrary, If I don't remove controller_name and then when I click link about, contact ... (not home), css and image path doesn't work. What's difference?


Tags for this Thread

Bookmarks

Posting Permissions

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