Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Link To CSS File With PHP

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #33244
    sebgonz
    Member

    Let me start off with a little bit of history… I’ve been asked to clean up the intranet pages at my place of employment. Currently, they’re some hacked together PHP that doesn’t have a template or anything, basically no style to the site and uses some god-awful table styles (no table styles?) to display the information.
    The basic structure of the site is fine, meaning the way it’s pulling information from the databases. What I want to do is clean it up, make it attractive to use, and create a template style structure to it a la WordPress. (header.php, sidebar.php, footer.php) Where I’m running into issues is with my includes and having my scripts and css linked properly. For example…

    This works fine when used on the root (in header.php), but when I try to use the same file in a different page, usually on a different level, I don’t get any stylesheet loading because the file is obviously not in the same level as the page I’m currently on.

    What I want to be able to do is something WordPress-esque like

    When I do this though, it doesn’t work. How can I get this to always find the root directory of the site (intranet) without breaking? I’d prefer to do it this way because, from what I’ve read, it’s more configurable over using direct paths.

    Any help would be appreciated. Oh… did I mention I’m developing this in XAMPP?

    #82161
    DboG
    Member

    The simplest solution would be to precede the CSS path with a /, which will always make the starting point the site root, regardless of where it’s being called.

    #82176
    ddliu
    Member

    Basicly, you can define a BASEURL and use it as a prefix with your assets.

    For example, you have a website with this url: http://example.com/blog, you can define it:


    define('BASEURL','/blog');

    or if your website is http://example.com, you can define it:


    define('BASEURL','');

    then in your template file ,use it like this:



    I’m using the similar way in some of my projects, and it worked perfect for me.

    #82205
    sebgonz
    Member

    @ddliu, Thanks! That looks like what I need to do. I’ll try it and report back.

    #82291
    sebgonz
    Member

    Where do I need to define BASEURL? Would that be in my index.php or header.php file?

    #82301
    noahgelman
    Participant

    Seriously, it’s best to just link to the root like DboG suggested. defining the base url in php is just adding another step and is pretty pointless

    #82356
    ddliu
    Member

    @sebgonz, you can define it any where before the template.


    @noahgelman
    , yes I agree that @DboG’s way is simple and strait-forward, but it’s not that flexible and not configurable. What if the app is not at the root URL? For example, it’s a news app located at http://www.example.cm/news, or a blog app located at http://www.example.com/blog, or even more, you have to change it to another location someday…

    Anyway, if your app is located at the root and will not be changed, @DboG’s way is recommended.

    Hope it’s clear.

    #82357
    noahgelman
    Participant

    @ddliu, why does it matter where it’s located? Linking to the root makes it irrelevant. The stylesheet would apply the same way to the index page as well as a page a hundred folders deep.

    #82371
    ddliu
    Member

    @noahgelman, yes I agree with you that it should be linking to the root.

    The only different point I mean is the flexibility.

    If your app don’t go into following situation, just forget it.

    You have an app hosted at http://www.example.com/myapp,
    your folder structure like this:


    myapp/
    index.php
    assets/
    css/
    main.css
    js/
    main.js

    So in your template you might define the assets like this:




    Someday you decided to change it to http://www.example.com/myapp-with-new-name,
    ok you have to find each template and change the links.

    Someday you decided to change it to http://www.example.com/apps/myapp…

    It’s best to make an application flexible and can be put in any location if possible I think. It’s the same thing like:

    1. You need to have a site url configured in WordPress.

    2. You need to define the attachment path in many of your applications.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.