• home
  • forum
  • my
  • kt
  • download
  • Create theme selection using PHP and CSS

    Author: 2008-08-28 10:54:10 From:

    Well for this tutorial we will create a theme selection, through the use of a php created css script. This is a fairly simple process. To start with we first create an external css style sheet, well we don't even need to do that. We just need to create a file that holds our style just as we like them. For this example I will use the following example;

    body
    {
    background-color: #000000;
    }


    Basically all that style does is change the background of the page to black. Not very impressive, though adequate for our needs. If we were to save this as say 'style.php' and try calling it using the following command;

    <link type="text/css" rel="stylesheet" href="style.php">

    We wouldn;t get a style that loaded. Basically this is because the output of this .php file is not formatted to .css. TO over come this we just need to include the following code at the beginning of the code, before there is any information outputted;

    Header ("Content-type: text/css");

    This basically formats the output so that it is accepted as a .css file. So the full code for our example would be as follows;

    <?
    Header (Content-type: text/css");
    ?>
    body
    {
    background-color: #000000;
    }


    I'm sure your asking yourself what is the use of this. We it has the following uses. For example say that you had multiple themes. You could store the values in either conditional statements or a mult-dimensional array, such as the following;

    <?
    Header ("Content-type: text/css");
    $array = array("black" => array("background" => "#000000", "textColor" => "#FFFFFF"), "white" => array("background" => "#FFFFFF", "textColor" => "#000000"));
    ?>
    body
    {
    background-color: #000000;
    }


    This array basically just allows us to change the background and texts colors. However at the moment it is still a static file. Now what if instead of calling style.php we were to call style.php?theme=white and then use $_GET['theme'} in our code. The result would probably be similiar to below;

    <?
    Header ("Content-type: text/css");
    $theme = $_GET['theme'];

    $array = array("black" => array("background" => "#000000", "textColor" => "#FFFFFF"), "white" => array("background" => "#FFFFFF", "textColor" => "#000000"));
    ?>
    body
    {
    background-color: <? echo $array[$theme]['background']; ?>;
    }


    Now this allows us to change what our theme is depending on what the value of theme is that we call. Now that might be interestsing, though it isn;t very helpful by itself. However consider the following scenario. Say you have a link on your page that allows the user to change the theme. Say this link takes you to a page where it sees what selection you picked and then assigns a keyword identifying this theme to a session. Then imagine that this page then takes you back to our original page where a piece of code then sees if there is a session present and if there is it calls the right theme, and if not it calls a default theme. This might look like the following three files;

    styles.php
    <?
    Header ("Content-type: text/css");
    $theme = $_GET['theme'];

    $array = array("black" => array("background" => "#000000", "textColor" => "#FFFFFF"), "white" => array("background" => "#FFFFFF", "textColor" => "#000000"));
    ?>
    body
    {
    background-color: <? echo $array[$theme]['background']; ?>;
    }


    index.php
    <?
    session_start();

    //Checks to see if there is a theme selected, if so use that theme otherwise use te default theme
    if($_SESSION['theme']){
    $theme = $_SESSION['theme'];
    }else{
    $theme = "black";
    }

    //This is the code that allows the theme selection to return back to this page
    $url = $_SEREVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    ?>
    <html>
    <head>
    <link type="text/css" rel="stylesheet" href="style.php?theme<? echo $theme; ?>">
    </head>
    <body>
    <a href="select.php?theme=black&url=<? echo $url; ?>"Black Theme</a>
    <a href="select.php?theme=white&url=<? echo $url; ?>"White Theme</a>
    </body>
    </html>


    select.php
    <?
    session_start();
    //Assign theme selection to session
    $_SESSION['theme'] = $_GET['theme'];

    //Return to previous page
    $return = "Location: " . $_GET['url'];
    return ($return);
    ?>


    Now this code should give you a basic theme selection. This can be expanded, to say allow users to have a semi-permanent selection through the use of cookies, or maybe even allowing users to create and use their own custom themes.

    Well thats all I hope you enjoyed this article, and that it was helpful.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (21)
      Cookies and Sessions (11)
      Counters (15)
      Database Related (20)
      Date and Time (13)
      Development (19)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot