duanpo6079 2012-07-12 15:41
浏览 106
已采纳

让PHP更改随机生成的CSS背景颜色

I realize that there have already been several threads on this type of question, but none that seemed to match mine. I have code that is correctly displaying a series of 6 numbers for a CSS background. Those 6 numbers are then stored into an array and the contents of that array are concatenated with a "#" sign in front of it. That, then is stored as another variable. Here:

$v = 0;
$array = array();
$tot;
$other0 = null;
$other1 = null;
$other2 = null;
$other3 = null;
$other4 = null;
$other5 = null;
$color;

for ($i=0;$i<6;$i++){ //loops 6 times to create 5 numbers
    $tot = rand(0, 15);

    if (($tot>9) && ($tot<16)){ //assigns 10 to "a" in hex
        if ($tot == 10){
            $tot = $other0;
            $other0 = "a";
            //echo $other0;
            $array[$v++] = $other0;
        }
        elseif ($tot == 11){ //assigns 11 to "b" in hex
            $tot = $other1;
            $other1 = "b";
            //echo $other1;
            $array[$v++] = $other1;
        }
        elseif ($tot == 12){ //assigns 12 to "b" in hex
            $tot = $other2;
            $other2 = "c";
            //echo $other2;
            $array[$v++] = $other2;
        }
        elseif ($tot == 13){ //assigns 13 to "b" in hex
            $tot = $other3;
            $other3 = "d";
            //echo $other3;
            $array[$v++] = $other3;
        }
        elseif ($tot == 14){ //assigns 14 to "b" in hex
            $tot = $other4;
            $other4 = "e";
            //echo $other4;
            $array[$v++] = $other4;
        }
        elseif ($tot == 15) { //assigns 15 to "b" in hex
            $tot = $other5;
            $other5 = "f";
            //echo $other5;
            $array[$v++] = $other5;
        }

    }
    else {
        //echo $tot;
        $array[$v++] = $tot; //if not, then just assign to array
    }
}
$var = "";
$color = "";
for ($t=0; $t<6;$t++){
    $var .= (string) $array[$t]; //displays the 6 numbers as one string
}
//var_dump($var); //for more visual reference, uncomment the var_dump
$color = "#" . $var;
echo $color;

That above is in PHP.

How do I get that variable to be displayed using CSS? Is it like (in the same PHP file): echo "<style>color:$color</style>";

or do I have to make a style.php for it to be referenced? If I do, how do I do that?

Many thanks!

  • 写回答

5条回答 默认 最新

  • dongqing8765 2012-07-12 15:44
    关注

    Well it looks like "<style>color:$color</style>"; will output broken html/css.

    Try amending

    echo '<style type="text/css"> body { background: '.$color.' } </style>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥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,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?