简体   繁体   中英

Dynamic CSS with PHP based on database

found a couple answers here on StackOverflow and used them as my models, but I must be missing something. I'm trying to set a couple of background colors dynamically in CSS based on what is in my database, but it's not working - when I check Inspect Element in Chrome, background-color has a line through it and a warning mark for 'Invalid property value'.

Here's my code; it's in two separate files - the first is in the header include file, and the second is in the linked .php / css-esque file.

Header include: [Edited 4/29 to include session code]

session_start();
// check if $_SESSION was set before
if (!isset($_SESSION['email'])) {
header("Location: bad_login.php");
exit();
}

$_SESSION['companyid'] = $_POST['companyid'];
$companyID = $_SESSION['companyid'];
$email = $_SESSION['email'];

require_once('../includes/_connection.inc.php'); 
$connect = dbConnect('read');
$sql = 'SELECT colorone, colortwo, logo
        FROM companies
        WHERE companyid = ' . $companyID;
$result = $connect->query($sql) or die(mysqli_error());
$row = $result->fetch_assoc();
$colorOne = '#' . $row['colorone'];
$colorTwo = '#' . $row['colortwo'];
$carrierLogo = '/companylogos/' . $row['logo'];

PHP/CSS file:

<?php header("Content-type: text/css"); 

?>

#main {
    width: 85%;
    margin: 0 auto;
    padding: 0.75em 0;
}

#colorOne {
    width: 100%;
    height: 12px;
    background-color: <?php echo $colorOne; ?>;
}

#colorTwo {
    width: 100%;
    height: 7px;
    background-color: <?php echo $colorTwo; ?>;
}

EDIT 4/29:

This is the CSS generated:

#main {
    width: 85%;
    margin: 0 auto;
    padding: 0.75em 0;
}

#colorOne {
    width: 100%;
    height: 12px;
    background-color: ;
}

#colorTwo {
    width: 100%;
    height: 7px;
    background-color: ;
}

I also echoed the variable back in the html so I know that there should be something in the variable. Should I be opening the database and assigning the variable inside the css.php file?

CSS/PHP is linked this way in header:

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

Instead of using the .css file extension, use .php

in the html file: is it linked to .php?

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

in the style.php add

<?php
    header("Content-type: text/css; charset: UTF-8");
?>

Now you can set up variables for whatever you like:

source

Edit:

Don't forget about session_start(); since you're using sessions (I don't understand how, since nothing gets posted to css/carrier.php you should rather have it in session from a different file & then just use the $companyID = $_SESSION['companyid']; $email = $_SESSION['email']; ).

is this the way your code looks?

        <?php
    session_start();
        header("Content-type: text/css; charset: UTF-8");
    $_SESSION['companyid'] = $_POST['companyid'];
    $companyID = $_SESSION['companyid'];
    $email = $_SESSION['email'];

    require_once('../includes/_connection.inc.php'); 
    $connect = dbConnect('read');
    $sql = 'SELECT colorone, colortwo, logo
            FROM companies
            WHERE companyid = ' . $companyID;
    $result = $connect->query($sql) or die(mysqli_error());
    $row = $result->fetch_assoc();
    $colorOne = '#' . $row['colorone'];
    $colorTwo = '#' . $row['colortwo'];
    $carrierLogo = '/companylogos/' . $row['logo'];
    ?>

#main {
    width: 85%;
    margin: 0 auto;
    padding: 0.75em 0;
}

#colorOne {
    width: 100%;
    height: 12px;
    background-color: <?php echo $colorOne; ?>;
}

#colorTwo {
    width: 100%;
    height: 7px;
    background-color: <?php echo $colorTwo; ?>;
}

The answer of yesitsme is correct. Other thing you can do is that each storage changes in the database, run the process of creating this "new" CSS file with the appropriate .css extension.

What if with every request you create a new CSS file? I mean, you have two paths, when creating the first call to the Web and update it from time to time, either, at the time you keep the data in the database associating it to a script.

With this new CSS and stored is generated through fwrite () and other functions that PHP has to manage files, keep the name of the CSS created in the BDD and then in your place the link as appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM