How can I change CSS link using external php

Discussion in 'PHP' started by seofighter, Jul 16, 2010.

  1. #1
    Hi,


    any one can please tell me how can i change CSS link using external php
    basically I am trying to change the link to my style sheet in the head of my index, by using an external php page with dropdown menu. Let's say someone selects "Style 3 - Green" from the dropdown and it automatically changes the link in the index to "style_green.css", is this possible, without using databases? I am trying to allow the user to select various css files without manually changing links in the index.
     
    seofighter, Jul 16, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Hmm, maybe include will suffice?
    
    if (style == 3) 
      include 'style_green.css';
    else .. etc etc;
    
    Code (markup):
     
    Rainulf, Jul 17, 2010 IP
  3. TheSal

    TheSal Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you want to make it changed when the user select the style, you need to use java script.
    You have to reload the page to change the style, so you can do this by sending a form to the same page, or to use session, thats gets the user choise.

    Any way, put your <style> tag into a php var and then echo it:
    
    if($_POST['style'] == 1){
    echo '<style type="text/css" media="screen">@import url(style1.css);</style>';
    }else{
    echo '<style type="text/css" media="screen">@import url(style2.css);</style>';
    }
    
    PHP:
    hope it helps...
     
    TheSal, Jul 17, 2010 IP
  4. savageman123

    savageman123 Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Instead of wrapping it inline set your headers to output css so it can be cached by the browser.

    header('Content-Type: text/css; charset: UTF-8');
    header('Cache-Control: must-revalidate');
    
    $expire_offset = 3600; // expires in 1 hour
    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire_offset) . ' GMT');
    Code (markup):
    Also, you can use javascript to change the css without reloading the page,

    <link id="css" href="mycss.php?number=1" type="text/css" rel="stylesheet">
    Code (markup):
    And then for your javascript event that is fired when the css is changed
    document.getElementById('css').href = 'mycss.php?number=2';
    Code (markup):
    You'd also want to store the new css selected in a cookie (or a session when the mycss.php file is hit) so you can output the newly selected style on each subsequent page load.
     
    savageman123, Jul 18, 2010 IP