du4822 2015-12-09 06:13
浏览 67

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

The idea is to have multiple stylesheets in a directory. The user will choose what stylesheet they want from a drop down menu that's called by the search_dir() function.

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

Keeping in mind I'm new to PHP, how would I get the styleseet to work once a user hits the submit button? Currently I'm trying this

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

..where power is the name of the select.

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

So once the user hits submit after selecting "styleone.css" the style would change. If they chose "styletwo", it would change to that style.

I hope I gave enough information for you guys to help me with this.

EDIT:

here is my select, option, ect section:

<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>

I hope this helps clarify more

  • 写回答

1条回答 默认 最新

  • douren5898 2015-12-09 06:31
    关注

    Two things to note: 1:- The link to the css should have the relative path by prefixing with "../css" as i see you are trying to scan a folder "../css"

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

    2:- iterate through the list of contests and add to the dropdown list. Note. as you are scanning the folder you may get the special folders as well in the list "." and ".." ( dot and double dot ), which you may want to ignore and strip out before adding to the option.

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画