如何在PHP页面中设置变量,将其传递给CSS文件并将其分配给单个CSS属性? [英] How do I set a variable in a PHP page, pass it to a CSS file and assign it to a single CSS attribute?

查看:211
本文介绍了如何在PHP页面中设置变量,将其传递给CSS文件并将其分配给单个CSS属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,FYI:下面引用的.php文件将是Serindipity博客的index.php,这是一个非常复杂的代码和文件混乱。



在我的index.php文件中,我想从URL中获取查询字符串(如果存在),并将其分配给一个变量(如 variable = self.location.search )。



我想将该变量赋给style.css文件中单个条目的单个属性,该文件由许多文件Serindipity利用生成的输出页面:

  body {
font-size:10pt;
margin:1;
background-color:#000000;
font-family:verdana,arial,helvetica,sans-serif;
margin-bottom:30px;
}

我只想更改背景颜色参数为上述多个条目中的一个,指向该变量的$值(它将是透明或null)。



<在这个过程中,我需要使用逻辑代码来确定是否有一个查询字符串。



如果是,请使 background-color = variable 。如果没有,请使 background-color =#000000



(当然,逻辑必须在CSS文件中执行。



这是否可能?



谢谢!

解决方案

我不熟悉你提到的引擎(Serindipity),但这应该是相当简单,只要你允许创建和编辑自己的Php脚本应该不重要,因为引擎不依赖于样式表的名称):



将样式表的文件结尾更改为

  .php 

,并且Web服务器将它识别为php脚本。设置mime类型为text / css like @Brighty写

  header('Content-type:text / css'); 

,用户永远不会知道差异(除非文件结尾是<$如果样式表的文件结尾为 .php

/ code>是一个问题,你可以总是使用.htaccess和一些聪明的正则表达式让用户相信他们看到一个 .css



编辑:此外,如果您有访问配置什么文件将被识别为php脚本,您可以设置您的服务器也识别 .css 作为一个php文件(据我所知不会是一个问题,因为php将只是折腾任何纯文本遇到的用户)。


First off, FYI: The .php file referenced below will be the index.php of a Serindipity blog, which is a very complex mess of code and files.

In my index.php file, I want to grab the query string, if one exists, from the URL and assign it to a variable (as in variable=self.location.search).

I want to then assign that variable to a single attribute of a single entry in a style.css file that is called by one of the many files Serindipity utilizes to generate the ouptput page:

body {
font-size: 10pt;
margin: 1;
background-color: #000000;
font-family: verdana, arial, helvetica, sans-serif;
margin-bottom: 30px;
}

I want to change only the background-color parameter of one of the many entries, shown above, to the $ value of that variable (which would be either "transparent" or null).

In the process, I need to employ logic code to determine if there is a query string at all.

If yes, make background-color=variable. If not, make background-color="#000000".

(Of course, the logic must be executed within the CSS file.)

Is this possible?

Thanks!

解决方案

I'm not familiar with the engine you mentioned (Serindipity), but this should be pretty straightforward, as long as you are allowed to create and edit your own Php-scripts (which really shouldn't matter as long the engine isn't dependent on the name of the style-sheet):

Change the file-ending of the style-sheet to

.php

and the webserver will recognize it as a php-script. Set the mime-type to text/css like @Brighty wrote

header('Content-type: text/css');

and the user will never know the difference (except, of course, the file-ending being .php).

If the style-sheet having a file-ending of .php is a problem, you can always use .htaccess and some clever regex to make the user believe they are seeing a .css.

Edit: Also, if you have access to configure what file/s will be recognized as php-scripts, you can set your server to also recognize .css as a php-file (which, as far as I know wouldn't be a problem, since php will just toss any plain text encountered out to the user).

这篇关于如何在PHP页面中设置变量,将其传递给CSS文件并将其分配给单个CSS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆