简体   繁体   English

PHP-动态更改变量

[英]PHP - Dynamically change variable

My website runs on a single page with dynamic CSS changes. 我的网站在具有动态CSS更改的单个页面上运行。 If I want to direct the client to a page content, all I do is giving the page link with passing CSS id. 如果我想将客户端定向到页面内容,我要做的就是给页面链接传递CSS ID。 ie: www.ilstours.net/index.php?css=1 即: www.ilstours.net/index.php?css = 1

index.php: index.php:

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/<?php echo $css; ?>.php" />
    </head>
    <body>
<button id="button1" type="button" onclick="index.php?css=2">Update</button> 'HERE
    </body>
    </html>

1.php: 1.php:

<?php
header("Content-type: text/css; charset: UTF-8");
$Color = "black";
?>
body {background-color:<?php echo $Color ?>;}

2.php: 2.php:

  <?php
    header("Content-type: text/css; charset: UTF-8");
    $Color = "red";
    ?>
    body {background-color:<?php echo $Color ?>;}

How can I switch to CSS file 2.php upon button click? 单击按钮后如何切换到CSS文件2.php?

当您通过url传递值时,请使用$ _GET访问它

<link rel="stylesheet" type="text/css" href="css/<?php echo $_GET['css']; ?>.php" />

index.php index.php

<?php
$css = 1; // default css (1)
if(isset($_REQUEST['css']) && ($_REQUEST['css']==1 || $_REQUEST['css']==2))
{
  $css = $_REQUEST['css']; // update by click on button (1 or 2)
}

?>
      <!DOCTYPE html>
        <html>
        <head>
        <link rel="stylesheet" type="text/css" href="css/<?php echo $css; ?>.php" />
        </head>
        <body>
            <button id="button1" type="button" onclick="location.href='index.php?css=1';">Update CSS 1</button>
            <button id="button1" type="button" onclick="location.href='index.php?css=2';">Update CSS 2</button>

        </body>
        </html>
if(isset($_GET['css'])) {
    echo '<link rel="stylesheet" type="text/css" href="css/'.$_GET['css'].'.php" />';

}

Better keep your css in single array like 最好将CSS保持在单个数组中

<?php
   $my_css = array("1"=>"1.css","2"=>"2.css",'deflt' => 'defualt.css');
?>

and include css like 并包括像

<link rel="stylesheet" type="text/css" href="css/<?php echo (isset($_GET['css']) && isset($my_css[$_GET['css']])) ? $my_css[$_GET['css']] : 'defualt.css'; ?>" />

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

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