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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示