【问题标题】:Getting PHP to change a randomly generated CSS background color让 PHP 更改随机生成的 CSS 背景颜色
【发布时间】:2012-07-12 10:05:04
【问题描述】:

我意识到这类问题已经有好几个主题了,但似乎没有一个与我的匹配。我有正确显示 CSS 背景的一系列 6 个数字的代码。然后将这 6 个数字存储到一个数组中,并将该数组的内容与前面的“#”符号连接起来。然后将其存储为另一个变量。这里:

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

以上是PHP。

如何使用 CSS 显示该变量?是不是像(在同一个 PHP 文件中):echo "&lt;style&gt;color:$color&lt;/style&gt;";

还是我必须创建一个 style.php 才能被引用?如果我这样做,我该怎么做?

非常感谢!

【问题讨论】:

  • 您需要在 html 中的 body 标记之前运行 php。然后你可以在页面中为body { background-color:&lt;?PHP echo $color; ?&gt; } 做一个内联样式
  • 你真的需要学习十六进制 :) 查看:php.net/manual/en/function.dechex.php
  • 天哪,我这辈子都去哪儿了?
  • @ChaseYuan 记住这一点:无论你想做什么,PHP 都有相应的功能。
  • 我建议使用 0 到 255 之间的 3 个随机生成的值,例如: $blub = "#".dechex(mt_rand(0, 255)).dechex(mt_rand(0, 255) ).dechex(mt_rand(0, 255));

标签: php css hex


【解决方案1】:
<?php
    $alpha = ["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"];
    $color = "#";
    for ($i=0; $i<6; $i++) {
        $index = rand(0,count($alpha)-1);
        $color .= $alpha[$index];
    }
?>
<html>
    <body style="background:<?php echo $color ?>">
        <?php echo $color ?>
    </body>
</html>

看来你把事情弄得比想象的要复杂一些。这是一个带有 html 集成的示例,正​​如您首先要求的那样。 :)

你在一个数组中声明你需要的所有元素,然后你循环 6 次以:生成一个随机索引,从数组中获取相应的元素并将它连接到你的颜色变量。

最后,如前所述,您可以在 html 文件中任何您需要的地方回显它。

【讨论】:

    【解决方案2】:

    你为什么不看看这些链接。

    Change background color of a page using php

    所以不要这样做:

    if blablabla {
        echo '<body style="background-color:white">'; 
    }
    else {  
        echo '<body style="background-color:orange">'; 
    } 
    

    这样做:

    echo '<body style="background-color:(variable)">'; 
    

    【讨论】:

      【解决方案3】:

      看来"&lt;style&gt;color:$color&lt;/style&gt;"; 会输出损坏的html/css。

      尝试修改

      echo '<style type="text/css"> body { background: '.$color.' } </style>';
      

      【讨论】:

      • 是的,我不小心点击了提交。 [已编辑]
      【解决方案4】:
      <body style="background-color:<?php echo $color; ?>;">
      
      
      content
      
      
      </body>
      

      style 属性允许您为元素定义自定义 css,css 属性、background-color 基本上定义了该元素的背景颜色...不言自明,我知道!

      【讨论】:

        【解决方案5】:

        应该是

        <style>
        body {
           background-color: "<?php echo $color; ?>";
        }
        </style>
        

        这需要在您的 &lt;head&gt; 元素内。

        【讨论】:

        • 嗯。这似乎不起作用,因为在定义之前调用了 $color。
        • 你在哪里定义 $color? PHP 应该在生成 HTML 之前全部执行。但是在回显之前需要定义 $color
        • 我在调用 php 之前生成了我的 html。我应该在函数之后移动它吗?
        • 如果你有一个包含 HTML 和 PHP 的 PHP 文件,所有的 PHP 都会先执行,不管 HTML 是否在文件中最先出现。它将首先执行所有 PHP。但是,存在的 PHP(除了 HTML)将按顺序运行。因此,生成 $color 变量的函数/代码需要存在于您回显它的上方。话虽如此,您最好使用不同的建议(如 Mahn 的)来生成颜色变量,因为听起来它实际上可能不是一种颜色。希望这会有所帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 2017-11-04
        • 2013-10-14
        • 2018-04-14
        • 2014-10-19
        相关资源
        最近更新 更多