www.webdeveloper.com
Results 1 to 10 of 10

Thread: Linking PHP files to nested files

  1. #1
    Join Date
    Nov 2011
    Posts
    82

    Linking PHP files to nested files

    I've built a site where I have a header.php file, index.php and footer.php. In the header.php file I am calling the .css files that I am using, which live in the root directory. I then have my work pages, which live in their own folders and each have an index file (mountain_bike/ is a folder that lives on the root with an index.php file for the mountain bike page). For some reason when I do this, I can access the same header and footer files despite being in the folder, but the css file is not being read. Is there another step I should take or another way of going about this? Thanks.

  2. #2
    Join Date
    Jun 2012
    Posts
    6
    Just use the exact root location instead of the relative root.

  3. #3
    Join Date
    Nov 2011
    Posts
    82
    What do you mean by that?

    In my header.php file I have this...

    HTML Code:
    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="stylesheet" type="text/css" href="portfolio_style.css">
    The header file and css files live on the root. The mountain_bike/index.php is still using the same header file as the home page. So I did this..

    PHP Code:
    <?php
    include("../header.php");
    ?>
    The header and footer both are linked to the mountain_bike/index.php, but the CSS is not being applied.

    Sorry if I'm duplicating efforts, wasn't sure what you meant.

  4. #4
    Join Date
    Jun 2012
    Posts
    6
    Quote Originally Posted by andy.richin View Post
    What do you mean by that?

    In my header.php file I have this...

    HTML Code:
    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="stylesheet" type="text/css" href="portfolio_style.css">
    The header file and css files live on the root. The mountain_bike/index.php is still using the same header file as the home page. So I did this..

    PHP Code:
    <?php
    include("../header.php");
    ?>
    The header and footer both are linked to the mountain_bike/index.php, but the CSS is not being applied.

    Sorry if I'm duplicating efforts, wasn't sure what you meant.
    Well It could be two things then.
    1. the href isn't correct.
    2. the php isn't referencing it correctly.

    In either case, you can use the EXACT location.

    i.e your website/location of file moving from the root.

    First make sure the href is referencing it correctly.
    If you narrow it down to the php, try using exact location.
    Or even try using inline to make sure the css is correct.

    So I guess it could be three things.

  5. #5
    Join Date
    Nov 2011
    Posts
    82
    Ok thanks. One other thing, could it be the fact that I'm building this site in a DEV folder? So I guess it's not actually on the root, but it's all inside the DEV.

  6. #6
    Join Date
    Jun 2012
    Posts
    6
    yes that would have something to do with it. when you do /x it looks in the current folder location for the file you are referencing.

  7. #7
    Join Date
    Nov 2011
    Posts
    82
    So I moved everything to the root and still nothing. Everything works fine if I have example.php, header.php and style.css living in the same directory. Example.php is calling header.php which calls style.css. As soon as I move example.php into a folder example/index.php the style sheet stops working. The header still works, I'm calling
    PHP Code:
    <?php
    include("../header.php");
    ?>
    I don't know if I'm missing something. It seems pretty straight forward but not working. The href is right and php looks right too.

  8. #8
    Join Date
    Nov 2011
    Posts
    82
    Is this really that difficult? :/

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,327
    Just a use a leading slash in the href property to indicate the CSS file's location relative to the web root:
    HTML Code:
    <link rel="stylesheet" type="text/css" href="/style.css" />
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Nov 2011
    Posts
    82

    resolved

    Thanks! That worked.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles