Page 1 of 1

[SOLVED] Dynamic CSS from mySQL

Posted: Sun Jun 27, 2004 8:48 am
by mm00re
hey to all,

i am developing an app that is basicaaly a frontend to a database, i would however like tyo be able to customise the CSS that maintains the whole of this app.

rather than having *alot* of tables i thought i would have one table per section
such as a table with tuples for the menu colours and text colours etc.,

anyways to avoid heaps of tables i thought i would still have a css file and have it read in and modified according to the query from the database.

therefore on startup it will show default values and when changed via a settings page, then upon a refresh it will reflect the new changes that have been made in the css file. the same file just with modified values.

anyways using this approach im trying to do a reg replace and it doesnt work :S

i thought if i added a comment to prefix what i wanted to change and have the preifx the same as the attribute name it would be very easier, but not so far it isnt.

any ideas pls --> i think eventually i will use preg_replace, however i may need some help on the pattern syntax there

Code: Select all

function searchCSSFile($search, $file = 'css/global.css'){
    
    if (file_exists($file)){
      
        $contents = @ file_get_contents($file);

        if ($contents != false){
            echo 'looking for: ' . $search.'<br>';                        
            if (ereg($search, $contents)){    
                // just trying to find it at the moment 
                // work on replace later                    
                echo $search . ' Found In File... ' . $file;
            }
            else{
                echo $search . ' Not Found In File... ' . $file;
            }
        }
        else{
            echo 'Can''t open: ' . $file . "\n";
        }
    }
    else{
        echo $file . "Doesn't exist. \n";
    }

    return $match;
  }

searchCSSFile("/*menu_text_col*/ color:");
CSS snippet:

Code: Select all

/*menu_text_col*/color: #666666;
any ideas other than this would be apprecated :D

kind regards,

g00fy

Posted: Mon Jul 12, 2004 4:24 pm
by hawleyjr
I store the users CSS values in a PHP Class and store the class on the db.

Here is a brief example:

Code: Select all

class UserCSS&#123; 

    var $a_bodyProperties=array(); 
    var $a_bgProperties=array(); 
    var $a_mainTableProperties=array(); 
    var $a_otherProperties=array(); 
    
    function UserCSS()&#123;  &#125; 
    
    function getA_BodyProperties()&#123; return $this->a_bodyProperties; &#125; 
    function getA_BgProperties()&#123; return $this->a_bgProperties; &#125; 
    function getA_MainTableProperties()&#123; return $this->a_mainTableProperties; &#125; 
    function getA_OtherProperties()&#123; return $this->a_otherProperties; &#125; 
    
    function setA_BodyProperties($x)&#123; $this->a_bodyProperties=$x; &#125; 
    function setA_BgProperties($x)&#123; $this->a_bgProperties=$x; &#125; 
    function setA_MainTableProperties($x)&#123; $this->a_mainTableProperties=$x; &#125; 
    function setA_OtherProperties($x)&#123; $this->a_otherProperties=$x; &#125; 
    
&#125;
You can then do something like this:

Code: Select all

//USER CSS
$a_bodyParam = array(
	'BGCOLOR'=>'FFFFFF',
	'FONTTYPE'=>'COMIC SANS',
	'FONTCOLOR'=>'D34KJ2');

//INSTANTIATE CLASS
$USERCSS = new UserCss();

//SET PARAMETERS
$USERCSS->setA_BodyProperties($a_bodyParam);

//SERIALIZE AND ADD SLASHES TO CLASS
//THIS IS NEEDED FOR STORING INTO DATABASE
$USERCSS = addslashes(serialize($USERCSS));

//INSERT QUERY
$qry = "INSERT INTO MY_TBL(USER_VAL) VALUES('$USERCSS')";

Posted: Mon Jul 12, 2004 5:04 pm
by pickle
ya, I'd do just what ~hawleyjr said. If, for example, you've got two table row classes, row1 and row2, then just store those values in a table, and retrieve them in the stylesheet. If I've got a big stylesheet, what I usually do is:

Code: Select all

<?
header('Content-type: text/css');
$colour1 = //Whatever you've pulled from the DB for row1;
$colour2 = //Whatever you've pulled from the DB for row2;

echo <<<CSS
.row1{
  bg-color:$colour1;
}
.row2{
  bg-color:$colour2;
}
CSS;
?>

Posted: Tue Jul 13, 2004 6:45 am
by mm00re
thanx to all :D

i actually came to a conclusio similar to what you have offered,

thanx again