dongpiao8821 2012-10-23 04:26
浏览 242
已采纳

有没有办法PHP可以控制CSS

1) database

2) php

3) css

4) html form

The user enters some properties for example background: red in the html form and it saves in the db, then php loades from the db the color option in background css... and then how can I put php variable in the css properties...

also some hostings does not allow to add handlers in .htaccess, is there other options?

  • 写回答

5条回答

  • dq804806 2012-10-23 04:33
    关注

    Yes, it is possible.

    You can serve a css file with php by setting the content-type.

    <?php header('content-type: text/css; charset=utf-8'); ?>
    

    You would output the css from the php file.

    You could store values in a mySQL or other table with a form, and later retrieve them with the mySQLi or other library.

    This is an example of the php script:

    <?
        header('content-type: text/css; charset=utf-8');
    
        $bgColor = "#FFF"; // Get it from database, I'm setting manually for this example
    ?>
    
    body{
          background-color:<?php echo "$bgColor" ?>;
    }
    

    Alternatively, you could output inline css on the page. Either directly in to a tag, or inline on elements.

    If really needed a native css file for some reason (I can't think of one), you could technically overwrite a specific css file using php's fwrite() function, though I wouldn't recommend doing it this way.

     <?php
    
          $filename = "phpstyle.css";
    
          $fp = fopen($filename, 'w');
    
          $bgcolor = "#FFF";
    
          $css  = "body{";
          $css .= "background-color:".$bgcolor.";"; 
          $css .= "}";           
    
          fwrite($fp, $css);
    
          fclose($fp);
    
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!