基于数据库的PHP动态CSS

时间:2022-12-05 11:53:49

found a couple answers here on * 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'.

在*上找到了一些答案,并将它们用作我的模型,但我肯定漏掉了什么。我试图根据我的数据库动态地在CSS中设置一些背景颜色,但它不起作用——当我检查Chrome中的Inspect元素时,background-color有一条线穿过它,并有一个“无效属性值”的警告标记。

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文件中,第二个文件在链接的.php / css文件中。

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

标题包括:[编辑4/29,包括会话代码]

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 / CSS文件:

<?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:

编辑:4/29

This is the CSS generated:

这是生成的CSS:

#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?

我还将变量回显到html中,这样我就知道变量中应该包含一些内容。我应该打开数据库并在css中分配变量吗?php文件吗?

CSS/PHP is linked this way in header:

CSS/PHP在header中是这样链接的:

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

2 个解决方案

#1


5  

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

不要使用.css文件扩展名,使用.php。

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

在html文件中:它是否链接到。php?

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

in the style.php add

的风格。php添加

<?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'];).

不要忘记session_start();因为您正在使用会话(我不知道如何使用,因为没有任何内容被发送到css/carrier。php应该从不同的文件中进行会话,然后使用$companyID = $_SESSION[' companyID '];电子邮件= _SESSION美元['邮件'];)。

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; ?>;
}

#2


0  

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.

是的,我的回答是正确的。您还可以做的另一件事是,数据库中的每个存储更改都运行创建这个具有适当. CSS扩展名的“新”CSS文件的过程。

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.

如果每个请求都创建一个新的CSS文件会怎么样?我的意思是,当您创建第一个Web调用并不时更新它时,您有两条路径,当您将数据库中的数据与脚本关联时。

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.

使用这个新的CSS并通过fwrite()和PHP必须管理文件的其他函数生成,保留在BDD中创建的CSS的名称,然后在适当的位置放置链接。

#1


5  

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

不要使用.css文件扩展名,使用.php。

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

在html文件中:它是否链接到。php?

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

in the style.php add

的风格。php添加

<?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'];).

不要忘记session_start();因为您正在使用会话(我不知道如何使用,因为没有任何内容被发送到css/carrier。php应该从不同的文件中进行会话,然后使用$companyID = $_SESSION[' companyID '];电子邮件= _SESSION美元['邮件'];)。

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; ?>;
}

#2


0  

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.

是的,我的回答是正确的。您还可以做的另一件事是,数据库中的每个存储更改都运行创建这个具有适当. CSS扩展名的“新”CSS文件的过程。

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.

如果每个请求都创建一个新的CSS文件会怎么样?我的意思是,当您创建第一个Web调用并不时更新它时,您有两条路径,当您将数据库中的数据与脚本关联时。

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.

使用这个新的CSS并通过fwrite()和PHP必须管理文件的其他函数生成,保留在BDD中创建的CSS的名称,然后在适当的位置放置链接。