Scarwolf Scarwolf - 2 months ago 14
PHP Question

Generate CSS classes from database?

It's possible to define a main color and a secondary color (hex-codes) in my application, those are saved to the db.

The secondary color for example is used for links. I don't want to say

<a href="#" style="color: $fromDatabase">Text</a>
but instead
<a href="#" class=secColor>Text</a>
where
.secColor
has something like

.secColor {
color: $fromDatabase;
}


I'm using Laravel btw.

Answer

You can include a .php file as a css by using following code:

index.html:

<!DOCTYPE HTML>
<html>
<head>
   <link rel="stylesheet" href="style.php">
</head>
<body>
<!-- stuff goes here -->
</body>
</html>

style.php:

<?php
  header("Content-type: text/css");
?>
//DB Query
.secColor{
   color: <?php echo $fromDatabase;?>
}
Comments