SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: CSS and PHP

  1. #1
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CSS and PHP

    I want to design one template,that swaps a few colors and an image depending on which page is accessed. Can anyone explain to me briefly the method of accomplishing this,or point me in the direction of a tutorial.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well considering you can put PHP anywhere in an HTML file (as long as the extension is .php of course) you should be able to do this easily if you have the style definitions in the document itself. For example:
    PHP Code:
    <style type="text/css">

    <?php

    if(condition) {
    echo 
    "body { background: #000; }";
    } elseif (
    condition) {
    echo 
    "body { background: #FFF; }";
    }

    ?>

    </style>
    I'm not sure whether it's necessary to escape characters such as { and } though in the echo statements. And you can call a different stylesheet depending on the page:
    PHP Code:
    <head>
    <?php

    if(condition) {
    echo 
    "<link rel=\"stylesheet\" href=""\"style1.css\" />";
    } elseif(
    condition) { 
    echo 
    "<link rel=\"stylesheet\" href=""\"style2.css\" />";
    }



    ?>
    </head>
    Or you can add a forcetype directive to an .htaccess file to make stylesheets parse PHP if you want to use external files but I'm not sure how to do that exactly.

    Hope this helps [img]images/smilies/smile.gif[/img]

    -Sam

    Edit:

    Sorry, not sure what's up here, but in the second example ""\" should be replaced with simply \"
    Sam Hastings

  3. #3
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand what you meant
    thanx for the reply

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im trying to do the same sort of thing really but need to store the path to the images and the values of the stylesheets in the database so they can be altered by site administrators.

    What is the best way to do this? Is there a way of making a call to the database which occurs only once, then store the values that I retrieve in a cookie / session cookie?

    Ive got the values stored in a confic.php file at the moment but I'd like to be able to alter the values via a web interface.


    Matt

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might be better off calling the database each time you need the information -- were a user browsing your site, for say an hour, during which the admins were updating path information / style sheets, he might get stuck on a non-working page, since his cookies are storing the wrong information.

    Just grab the information from the DB at the top of each script, all in one SQL call -- it shouldn't add too much overhead to your script.

    Or, build into your web-interface a feature to update your config.php with static values of these paths / style sheets every time the admins change the database. Then, just require_once("globals.php") in all your scripts. This would save database calls
    My name is Steve, and I'm a super-villian.

  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Browsing this might help you. It's a dynamically built CCS external file that allows the site admins to cutomize themes for the site.
    PHP Code:
    <?php
      
    /*  General style sheet for forms */
      
    include('../includes/session_handler.php');
      
    session_start();
      
    $themeVars = array('themeId',
                         
    'bodyBgColor',
                         
    'bodyTextColor',
                         
    'textSize',
                         
    'errorColor',
                         
    'boxBgColor',
                         
    'boxBorderColor',
                         
    'boxBorderWidth',
                         
    'boxTextColor',
                         
    'linkColor',
                         
    'vLinkColor',
                         
    'hoverColor');
      if(!
    $_SESSION['themeId'])
      {
        
    $query 'SELECT * FROM tblThemes
                  WHERE
                    fldActive = "Y"
                    '
    ;
        include(
    '../includes/pquery.php');
        
    $row mysql_fetch_array($result);
        for(
    $i 0$i count($themeVars); $i++)
        {
           
    $_SESSION[$themeVars[$i]] = $row['fld'.ucfirst($themeVars[$i])];
        }
      }
      foreach(
    $themeVars as $var)
      {
        $
    $var $_SESSION[$var];
        
    //echo $var.' = '.$$var.'<br />';
      
    }
      
    header('Content-type: text/css');
      echo 
    '
        body {
          background-color: #'
    .$bodyBgColor.';
        }
        .error {
          color:  #'
    .$errorColor.';
        }
        .formText {
          font-family:  Helvetica;
          font-size:    '
    .$textSize.'pt;
          color:        #'
    .$bodyTextColor.';
        }
        .bold {
          font-weight: bold;
        }
        input, select, radio {
          border:     1px solid #'
    .$boxBorderColor.';
          font-size:  '
    .$textSize.'pt;
        }
        A {
          color: #'
    .$linkColor.';
        }
        A:link {
          text-decoration: none;
          color:  #'
    .$linkColor.';
        }
        A:visited {
          text-decoration: none;
          color: #'
    .$vLinkColor.';
        }
        A:hover {
          text-decoration: underline;
          color: #'
    .$hoverColor.';
        }
        table.boxed {
          background-color: #'
    .$boxBgColor.';
          border:       '
    .$boxBorderWidth.'px solid #'.$boxBorderColor.';
          font-family:  Helvetica;
          font-size:    '
    .$textSize.'pt;
          text-align:   left;
          color:
        }
        '
    ;
    ?>
    John

  7. #7
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above file is named user.php, and it is called by all the pages that use it this way:
    PHP Code:
          <head>
            <
    title>Untitled Document</title>
            <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <
    link rel="stylesheet" href="css/user.php" type="text/css" />
          </
    head
    John

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your suggestions lieut data and PHP John.... I'm gonna start working on it later tonight...

    ..... you'll no doubt hear from me again ... he he

    Matt

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another quick question though PHP John,

    What does the content of ../includes/session_handler.php look like.?

    Sorry for askin such a cheeky question>?

  10. #10
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "session_handler.php" file is a custom session handler that uses the database instead of files for sessions. You don't need it in order to take advantage of session. It's just my preference. If you'd like, I'll post the code for you.
    John

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...hmmmm sounds interesting PHP John...

    I'd love to see the code when you've got a minute or two to post it up here.

    Thanks in advance
    Matt

  12. #12
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go:
    PHP Code:
    <?php
      
    /* DATABASE SPECS:
        CREATE TABLE tblSessions(
                                fldSession_key CHAR(32) NOT NULL PRIMARY KEY,
                                fldSession_end INT(11) UNSIGNED NOT NULL,
                                fldSession_value TEXT NOT NULL
                                );
      */
      // custom session handling functions
    //  include('db_vars.php');
     
    $HOST           'localhost';
     
    $DB_NAME        '[databaseName]';
     
    $USER           '[userName]';
     
    $PASS           '[password]';
     
    $DB_CONNECTION  '';
     
    $SESSION_LIFE   get_cfg_var('session.gc_maxlifetime');
      
    // This function simply opens a persistant connection to the database
      
    function session_open($save_path$session_name)
      {
       global 
    $HOST$DB_NAME$USER$PASS$DB_CONNECTION;
       if(!
    $DB_CONNECTION mysql_pconnect($HOST$USER$PASS))
       {
        echo 
    "<li>Can't connect to $HOST as $USER";
        echo 
    "<li>mySQL error: ".mysql_error();
        die;
       }
       if(!
    mysql_select_db($DB_NAME$DB_CONNECTION))
       {
        echo 
    "<li>We were unable to connect to the session database.";
        die;
       }
       return 
    true;
      }
      
    // This function does nothing, but is required
      
    function session_close()
      {
       return 
    true;
      }
      
    // This function reads the session information from the database
      
    function session_read($session_key)
      {
       global 
    $sessions$userId;
       
    $session_key addslashes($session_key);
       
    $session_session_value mysql_query("SELECT fldSession_value FROM tblSessions WHERE fldSession_key = '$session_key'") or die(mysql_error());
       if(
    mysql_num_rows($session_session_value)==1)
       {
        return 
    mysql_result($session_session_value0);
       }
       else
       {
        return 
    false;
       }
      }
      
    // This function writes the session information to the database and
      // assigns a value to the variable $session
      
    function session_write($session_key$val)
      {
       global 
    $session;
       
    $session_key addslashes($session_key);
       
    $val addslashes($val);
       
    $session mysql_result(mysql_query("SELECT COUNT(*) FROM tblSessions WHERE fldSession_key = '$session_key'"),0);
       if(
    $session == 0)
       {
        
    $return mysql_query("INSERT INTO tblSessions (fldSession_key, fldSession_end, fldSession_value)
                VALUES (
                '
    $session_key',
                UNIX_TIMESTAMP(NOW()),
                '
    $val'
                )"
    )
            or die(
    mysql_error());
       }
       else
       {
        
    $return mysql_query("UPDATE tblSessions
                        SET fldSession_value = '
    $val',
                        fldSession_end = UNIX_TIMESTAMP(NOW())
                        WHERE fldSession_key = '
    $session_key'")
            or die(
    mysql_error());
        if(
    mysql_affected_rows() < 0)
        {
         echo 
    "Was not able to update tblSession fldSession_value for fldSession $session_key";
        }
       }
       return 
    $return;
      }
      
    // This function wipes out the session information from the database
      
    function session_destroyer($session_key)
      {
       global 
    $session;
       
    $session_key addslashes($session_key);
       
    $return mysql_query("DELETE FROM tblSessions WHERE fldSession_key = '$session_key'")
        or die(
    mysql_error());
       return 
    $return;
      }
      
    // This function wipes out the session information from the database when the
      // expiration time of the session has passed
      
    function session_Gc($session_lifetime)
      {
       global 
    $session;
       
    $expiration_time time() - $session_lifetime;
       
    $return mysql_query("DELETE FROM tblSessions WHERE fldSession_end < $expiration_time")
         or die(
    mysql_error());
       return 
    $return;
      }
      
    session_set_save_handler(
                             
    'session_open',
                             
    'session_close',
                             
    'session_read',
                             
    'session_write',
                             
    'session_destroyer',
                             
    'session_Gc'
                             
    );
    ?>
    John

  13. #13
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And this is the "pquery.php" file:
    PHP Code:
    <?
    // This is the query for the persistant DB connection created by
    // session_handler.inc
      
    $error  '';
     if(!
    $result mysql_query($query$DB_CONNECTION))
     {
       echo 
    mysql_error().'<br />'.$query;
       exit;
     }
    ?>
    John


Bookmarks

Posting Permissions

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