如何从php中的一个目录中调用多个CSS样式表


how do I call multiple CSS stylesheets from a directory in php

这个想法是在一个目录中有多个样式表。用户将从search_dir()函数调用的下拉菜单中选择他们想要的样式表。

$search_dir = '../css';
$contents = scandir($search_dir);

请记住,我是PHP的新手,一旦用户点击提交按钮,我将如何让styleseet工作?目前我正在尝试这个

<link href="<?php echo $_POST["power"]; ?>" rel="stylesheet">

其中power是选择的名称。

<select class="" id="power" name="power" tabindex="1" >

因此,一旦用户在选择"styleone.css"后点击提交,样式就会改变。如果他们选择了"styletwo",它就会变成那个样式。

我希望我给了你们足够的信息来帮助我。

编辑:

这是我的选择、选项等部分:

<label for="power">Choose your POWER!!</label>
        <select class="" id="power" name="power" tabindex="1" >
        <?php
        echo '<option value="">Choose Here!!</option>';
        foreach($contents as $item):
            if((is_file($search_dir.'/'.$item)) AND (substr($item, 0, 1) != '.')):
                if($item == $_POST["power"]):
                    echo "<option value='"$item'" selected='"selected'"> $item </option>";
                else:
                    echo "<option value='"$item'"> $item </option>";
                endif;
            endif;
        endforeach;
        ?>
        </select>

我希望这有助于澄清更多

需要注意的两件事:1:-指向css的链接应该有相对路径,前缀为"../css",因为我看到你正在尝试扫描文件夹"../css"

<link href="../css/<?php echo $_POST["power"]; ?>" rel="stylesheet">

2:-遍历竞赛列表并添加到下拉列表中。笔记当你扫描文件夹时,你可能会在列表"."answers".."(点和双点)中得到特殊文件夹,你可能想忽略这些文件夹,并在添加到选项之前将其删除。

<form method="post">
<select class="" id="power" name="power" tabindex="1" >
<?php
        for ( $i=0; $i<count($contents); $i++ ) {
                echo '<option value="' . $contents[$i] . '">' . $contents[$i] . '</option>';
        }
?>
</select>