基于PHP字符串查询更改CSS
我需要根据URL中的查询字符串更改网页上的CSS。。。但我是PHP新手,还没有找到足够的例子来说明如何使用PHP基于PHP字符串查询更改CSS,php,css,Php,Css,我需要根据URL中的查询字符串更改网页上的CSS。。。但我是PHP新手,还没有找到足够的例子来说明如何使用PHP 基本上,当用户单击菜单链接,然后发送到“aboutUs.php”页面时,我需要aboutUs.php页面上的CSS根据字符串查询进行更改。您可以将style.CSS更改为style.php,您可以将其作为样式表加载到页面中 您需要在php文件中包含头类型: <?php header("Content-type: text/css"); ?> 然后动态生成内
基本上,当用户单击菜单链接,然后发送到“aboutUs.php”页面时,我需要aboutUs.php页面上的CSS根据字符串查询进行更改。您可以将
style.CSS
更改为style.php
,您可以将其作为样式表加载到页面中
您需要在php文件中包含头类型:
<?php
header("Content-type: text/css");
?>
然后动态生成内容
希望我没有误解这个问题。这可以帮助您更改css文件的内容
如果您有多个css文件,并且希望根据参数将其中一个导入到
aboutus.php
,则情况就不同了。在程序的头部(包括css样式表),执行一个简单的If-else:
if (isset($REQUEST['query_string'])) {?>
stylesheet link
<?php } else { ?>
stylesheet link 2
<?php } ?>
if(isset($REQUEST['query\u string']){>
样式表链接
样式表链接2
您可以将所选CSS保存到:
然后可以这样读取样式:$\u COOKIE['Style']
->样式设置为“黑色”不是很好的解决方案,但例如,类似的东西可以工作
<? php
if($str=="main"){
echo '<link rel="stylesheet" type="text/css" media="all" href="/css/main.css" />'
}
else if($str=="other"){
echo '<link rel="stylesheet" type="text/css" media="all" href="/css/other.css" />'
}
?>
如果我正确理解您的需求:
if (isset($_GET['query'])){
$query = $_GET['query'];
}else{
$query = NULL;
}
然后像这样:
if ($query == 'x'){
echo "
<style>
Whatever style you need...
</style>
";
}elseif ($query == 'y'){
echo "
<style>
Whatever other style you need...
</style>
";
}else{
echo "
<style>
Whatever default style you need...
</style>
";
}
if($query=='x'){
回声“
无论你需要什么风格。。。
";
}elseif($query==“y”){
回声“
无论你需要什么样的风格。。。
";
}否则{
回声“
无论你需要什么样的默认样式。。。
";
}
当然,你可以回显样式表链接而不是样式标签,但逻辑是一样的。知道你问题的目的,从,我会把你所有的常用css放在样式表中,只需为你关心的元素调用特定的显示属性。让你所有的元素显示:无你的主要样式表
<link type="text/css" rel="stylesheet" href="mainStyleSheet.css" />
<style type="text/css">
<?php
if (isset($_GET['section'])){
// Sanitize
$section = preg_replace('/[^a-zA-Z]/', $_GET['section']);
echo '#' . $section . ' { display: block; }';
}
?>
</style>
在这种情况下,section
是一个参数,设置为ourMisson
,ourHistory
等,如下所示:
if ($query == 'x'){
echo "
<style>
Whatever style you need...
</style>
";
}elseif ($query == 'y'){
echo "
<style>
Whatever other style you need...
</style>
";
}else{
echo "
<style>
Whatever default style you need...
</style>
";
}
http://beta.***.com/aboutUs.php?section=ourMission
您需要创建一个带有头文本/css的php文件,您可以使用get参数调用该文件。
例如:
$style=$_GET[“style”];
标题(“内容类型:文本/css”);
如果($style==“蓝色”)
echo“.class{背景色:蓝色;}”;
其他的
echo“.class{背景色:白色;}”;
使用get参数调用css
+1我误解了这个问题。:)我认为他希望css内容根据参数而改变。因此,为了理解解决方案,完整的PHP代码块应该是:不要使用$\u REQUEST,使用$\u GET或$\u POST,无论什么。错误做法:)不,使用REQUEST来实现这一点是完全正确的e、 “不要使用请求,这是错误的做法”没有任何解释是不好的建议,而且很容易出错。这似乎应该行得通……但我的php有点问题。它现在正在破坏页面?回送未初始化的输入会为通过恶意URL进行的注入攻击打开大门。这很公平。因为他只需要字母字符,这很简单。他还可以测试一次首先查看有效选项列表。
if ($query == 'x'){
echo "
<style>
Whatever style you need...
</style>
";
}elseif ($query == 'y'){
echo "
<style>
Whatever other style you need...
</style>
";
}else{
echo "
<style>
Whatever default style you need...
</style>
";
}
<link type="text/css" rel="stylesheet" href="mainStyleSheet.css" />
<style type="text/css">
<?php
if (isset($_GET['section'])){
// Sanitize
$section = preg_replace('/[^a-zA-Z]/', $_GET['section']);
echo '#' . $section . ' { display: block; }';
}
?>
</style>