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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    PHP and Paths in CSS

    Is there a way to set your path to image files in CSS dynamically with PHP.

    Example:

    Instead of this...

    Code:
    #menu a {
    	background: url("../images/button.jpg") 0 0 no-repeat;
    }
    
    
    I want to do something like this...
    
    
    Code:
    #menu a {
    	background: url("<?php echo $company['company_ttracker_path']; ?>images/button.jpg") 0 0 no-repeat;
    }

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    yes but for that to parse you would need to change from style.css to style.php and link to it normally.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Also, don't forget that on your actual PHP page (that uses your CSS file),
    you can also do an "inline CSS" style ... thus, you specifically change
    one of the tags, example:

    <div id="menu" style="background: url("<?=$company['company_ttracker_path'];?>") 0 0 no-repeat;">

    The inline coding will take precedence over the one in the .css file.

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thank you all for the help!!!

  • #5
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    You also need to change the MIME type if it's a PHP extension. Put header('Content-type: text/css') at the top (in PHP tags).

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thanks for the help!

    I am linking to the file like this from my main page...

    Code:
    <link rel="stylesheet" href="<?php echo $company['company_ttracker_path']; ?>css/main_ie7.php" type="text/css" media="all"/>
    And I put this at the top of the code in main_ie7.php and renamed my file to main_ie7.php

    Code:
    <?
    header("Content-type: text/css")
    ?>
    The CSS works fine, but the paths are not working.

    Code:
    	background-image: url("<?php echo $company['company_ttracker_path']; ?>images/dsg_portal_14.jpg");
    Any thoughts?

    Thanks again for any help!

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Maybe you're missing a slash?

    background-image: url("<?php echo $company['company_ttracker_path']; ?>/images/dsg_portal_14.jpg");

  • #8
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thought that too, but I checked it and it is there...

  • #9
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Is $company['company_ttracker_path']; being set in main_ie7.php?

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    This sounds like trying to hammer a nail with a pair of scissors.

    What I usually end up using when there is a dynamic path in my URL, is the <base> element. Both FireFox and IE are good at following this element, and it makes for a lot cleaner CSS and HTML.

    Do a quick google on the <base> element, and try it out. This would prevent you from having to put PHP into every CSS/etc.

  • #11
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Yeah I'd also check out the base tag, I use it on my own site and it's quite useful (that's why it's there for you to use).

  • #12
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Gah - wrong thread
    Last edited by meth; 08-25-2007 at 12:04 PM. Reason: :(
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

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