简体   繁体   English

我怎样才能用php生成css?

[英]How can I use php to generate css?

In my first version of this question, I did not offer enough information, I am sorry. 在我这个问题的第一个版本中,我没有提供足够的信息,对不起。 Here is a clearer version of what help I need. 这是我需要帮助的更清晰的版本。 Many of your answers are close already, this will help: 你的许多答案已经很接近,这将有助于:

Much More Specific Version of This Question: 这个问题的更具体版本:

I've decided to use php with css to make my pages work more efficiently than they would if I were to achieve the same outcome with javascript. 我已经决定使用php和css来使我的页面比使用javascript实现相同结果时更有效。 This is not important. 这并不重要。 What's important is that the two methods that I have tried to do this (as shown in a tutorial) are: 重要的是我尝试这两种方法(如教程中所示)是:

adding 加入

AddType application/x-httpd-php .css

to my .htaccess 到我的.htaccess

Then, when the style sheet showed up in the page's sources but did not have an affect on the page I tried using 然后,当样式表出现在页面的源中但对我尝试使用的页面没有影响

<link rel="stylesheet" href="style.php" media="screen">

to link my stylesheet as a php file, as suggested by this tutorial. 将我的样式表链接为php文件,如本教程所示。 http://www.phpro.org/articles/Embedding-PHP-In-CSS.html http://www.phpro.org/articles/Embedding-PHP-In-CSS.html

Here was the css that I was testing with the php variables: 这是我用php变量测试的css:

<?php
/*** set the content type header ***/
header("Content-type: text/css");

/** set the paragraph color ***/
$para_color = '#0000ff';

/*** set the heading size ***/
$heading_size = '2em';

/*** set the heading color ***/
$heading_color = '#c0c0c0';
?>

p{
    color: <?php echo $para_color;  ?>;
    font-weight: bold;
    font-size: 1.2em;
    text-align: left;
}       

h1{
    color: <?php echo $heading_color; ?>;
    font-size = <?php echo $heading_size; ?>;
    text-align: centre;
}

Both methods gave me the same outcome, with the css showing up exactly as is should as a stylesheet source, but not showing up on the page UNLESS I made a change to the style in the page's dev tools, after which the page would style just fine. 这两种方法都给了我相同的结果,css完全按照样式表的来源显示,但没有显示在页面上,除非我对页面的开发工具中的样式进行了更改,之后页面将样式化精细。

Question: Why is this happening, and how do I fix it? 问题:为什么会发生这种情况,我该如何解决?

Old Version: 旧版:

The question here is a simple one: 这里的问题很简单:

How can I use PHP in CSS? 我怎样才能在CSS中使用PHP?

I am creating a website where it will be much more efficient both in time and in processing speed for me to use php within CSS to achieve various desired results, rather than using javascript. 我正在创建一个网站,它在时间和处理速度上都会更有效率,我可以在CSS中使用php来实现各种所需的结果,而不是使用javascript。

I'm told that this can be done. 我被告知这可以做到。 How? 怎么样?

Your question is worded incorrectly and has confused a lot of people. 你的问题措辞错误,让很多人感到困惑。 What you want is a PHP file that will "generate" a CSS file so to speak. 你想要的是一个PHP文件,可以“生成”一个CSS文件。 So instead of using <link> for a CSS file, you will use it to link to a PHP file which outputs CSS. 因此,不使用<link>作为CSS文件,而是使用它链接到输出CSS的PHP文件。 As shown in the tutorial you posted. 如您发布的教程中所示。

<?php
/*** set the content type header ***/
header("Content-type: text/css");

/***
query the database for the background color
provided you have a valid MySQL connection or whatever you are using.
***/
$background_color = mysqli_query($dbc, "SELECT `background_color` FROM user_settings WHERE user_id = 1");

/** set the paragraph color ***/
$para_color = '#0000ff';

/*** set the heading size ***/
$heading_size = '2em';

/*** set the heading color ***/
$heading_color = '#c0c0c0';
?>

html{
    background: <?php echo $background_color; ?>
}

p{
        color: <?php echo $para_color;  ?>;
        font-weight: bold;
        font-size: 1.2em;
        text-align: left;
}       

h1{
        color: <?php echo $heading_color; ?>;
        font-size = <?php echo $heading_size; ?>;
        text-align: centre;
}

Now in your PHP file, you would use something like this. 现在在你的PHP文件中,你会使用这样的东西。

<link rel="stylesheet" href="style.php" media="screen">

Edit: Added hypothetical MySQL query on how to retrieve a user's preference. 编辑:添加了关于如何检索用户偏好的假设MySQL查询。 Cannot be thorough due to the vagueness of the question. 由于问题的模糊性,不能彻底。

in your html file: 在你的html文件中:

<link href="testCSS.php" rel="stylesheet">

in the testCSS.php file: 在testCSS.php文件中:

<?php
header("Content-type: text/css");
echo "body,html{background-color:silver; height:100%;color:white}";
?>

This is just a simple example. 这只是一个简单的例子。 You can always complicate the way of generating the stylesheet. 您总是可以使生成样式表的方式复杂化。

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the look and formatting) of a document written in a markup language. 层叠样式表(CSS)是一种样式表语言,用于描述用标记语言编写的文档的表示语义(外观和格式)。 more info : http://en.wikipedia.org/wiki/Cascading_Style_Sheets CSS is not a programming language, and does not have the tools that come with a server side language like PHP. 更多信息: http//en.wikipedia.org/wiki/Cascading_Style_Sheets CSS不是一种编程语言,并且没有PHP等服务器端语言附带的工具。 However, we can use Server-side languages to generate style sheets. 但是,我们可以使用服务器端语言来生成样式表。

<html>
<head>
<title>...</title>
<style type=”text/css”>
table {
margin: 8px;
}

th {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
background: #666;
color: #FFF;
padding: 2px 6px;
border-collapse: separate;
border: 1px solid #000;
}

td {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
border: 1px solid #DDD;
}
</style>
</head>
<body>
<?php>
echo "<table>";
echo "<tr><th>ID</th><th>hashtag</th></tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr onmouseover=\"hilite(this)\" onmouseout=\"lowlite(this)\"><td>$row[0]</td>                <td>$row[1]</td></tr>\n";
}
echo "</table>";
?>
</body>
</html>

PHP is a server side language whereas CSS is style sheet but you can create some conditional checks and play with classes . PHP是服务器端语言,而CSS是样式表,但您可以创建一些条件检查并使用classes

Just an example: 举个例子:

<span <?php if($a == 1) { echo "class='left'" ; } ?>This is some text </span>

In your CSS: 在你的CSS中:

.left{
//some css work here
}

So in this way you can apply css to any element on the basis of conditional statements 因此,通过这种方式,您可以在条件语句的基础上将css应用于任何元素

How about the following in the header of your file: 如下文件标题中的以下内容:

<style type=”text/css”>
   table {
   margin: <?php echo rand(1,5);?>px;
   }

This would include "inline" styling (in this case, the margin of the table element is determine programmatically by choosing a random number between 5 and 10.). 这将包括“内联”样式(在这种情况下,通过选择5到10之间的随机数来以编程方式确定表元素的边距)。 It's a trivial example but I believe this is what you are after. 这是一个微不足道的例子,但我相信这就是你所追求的。 Without more information provided it's hard to be more helpful. 如果没有提供更多信息,很难提供更多帮助。

Obviously you want your php to do something more sensible - but the idea (that php generates some of the css instructions) seems to be what you are after. 显然你希望你的php做一些更明智的事情 - 但是这个想法(php生成一些css指令)似乎就是你所追求的。

You can make your css file a php one. 你可以使你的CSS文件成为一个PHP文件。

For example, 例如,

default.css.php

And in there, you'd have 在那里,你有

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


body {
 background: <?=$color?>;
}

More detailed info here: http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/ 更详细的信息: http//net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/

如果您在样式标记中直接在php文件上编写css部分而不是在外部包含样式表,那么这是可能的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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