Grow your CSS skills. Land your dream job.

Use PHP to place a css file link in the header??

  • # September 19, 2012 at 12:31 pm

    Hey everyone

    I’m writing a small PHP script for someone who will just use a PHP include to run the script. The thing is that I need to supply some CSS for the script to use when it runs. Does anyone know how I can use PHP to automatically place the link tag with the link for the CSS file into the head section of the page that calls it because I really don’t want to have to use inline CSS as that is messy.

    Cheers everyone.

    # September 19, 2012 at 1:03 pm

    @blackhawkso Not sure why you’d want to do this. You can just put what I have below in the head section. You wouldn’t need to include it unless I’m not understanding your issue.

    ` `

    # September 19, 2012 at 1:27 pm

    @ChristopherBurton Hey Chris thanks for a reply.

    The issue is that I writing a file for the guy called calendar.php, now he can use this file anywhere he wants in any other script by using a PHP include. Now I could make the outputted html from the PHP script have a head tag etc etc so that the link for the CSS file goes inside a head tag like it should but I don’t know the page that he is going to use the script on and I don’t want to insert extra html tags into the middle of his page and make his code messy, so what I want to do is if he had a file called lets say index.php and in that file he used < ?php include(calendar.php); ?> I would want to place the link for the CSS file into the head tag of the index.php page. Clear as mud? lol

    # September 19, 2012 at 1:31 pm

    @blackhawkso That is understandable but why don’t you ask him where he is going to use it? Shouldn’t you know as you’re developing it for him? You could also use an if statement. Like so:

    if page is index.php

    echo stylesheet

    else

    do nothing

    # September 19, 2012 at 1:35 pm

    @ChristopherBurton I was just asked to throw together a simple calendar script for him as he has a big job on (I don’t know what the job is) and he is running out of time and I owed him a favour.

    # September 19, 2012 at 1:45 pm

    @blackhawkso You can either attach it to every page or you can write an if else statement. If the css file isn’t large, don’t worry about it.

    Updated:

    if ($url == “http://www.sample.com/test.php”) {
    echo ‘ ‘;
    };

    # September 19, 2012 at 1:47 pm

    @ChristopherBurton right ok cheers

    # September 19, 2012 at 1:50 pm

    @blackhawkso Check the code again above. It seemed to have been cut off previously.

    # September 21, 2012 at 1:30 am

    You don’t need the else { } in this case, though. If it doesn’t meet the condition of the if, it will already do nothing.

    # September 21, 2012 at 3:29 am

    In the above link, you can attach the if part to all pages, but you just remove the else part because it won’t do anything even if that if part is failed, specifically no need to mention.

    # September 21, 2012 at 9:13 am

    I didn’t know that. Thanks for the info @kevinvance @Dhanishta. I updated the code above.

    @blackhawkso Let us know if this solved your problem.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".