0

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!

12
  • you would need to run the php before the body tag in the html. you could then do an inline style in the page for body { background-color:<?PHP echo $color; ?> } Commented Jul 12, 2012 at 15:43
  • 5
    You really need to learn hexadecimal :) check out: php.net/manual/en/function.dechex.php Commented Jul 12, 2012 at 15:44
  • oh my goodness where has this been all my life? Commented Jul 12, 2012 at 15:51
  • @ChaseYuan Remember this: whatever you want to do there is a function for that in PHP. Commented Jul 12, 2012 at 15:52
  • 1
    I'd suggest using 3 randomly generated values between 0 and 255, for instance: $blub = "#".dechex(mt_rand(0, 255)).dechex(mt_rand(0, 255)).dechex(mt_rand(0, 255)); Commented Jul 12, 2012 at 16:02

5 Answers 5

1

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

Try amending

echo '<style type="text/css"> body { background: '.$color.' } </style>';
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I hit submit by accident. [Edited]
1

It should be

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

This needs to be within your <head> element.

4 Comments

Hm. This doesn't seem to work because $color is called before it is defined.
Where are you defining $color? The PHP should all be executed before the HTML is generated. But $color will need to be defined before you echo it
I have my html generated before the php is called. Should I move it after the function?
If you have a PHP file that contains HTML and PHP, all the PHP will execute first, it doesn't matter whether or not the HTML appears first in the file. It will execute all PHP first. The PHP that exists (aside from the HTML) will run sequentially however. So the function / code that generates the $color variable, will need exist above where you echo it. With all that being said, you maybe better off using a different suggestion (like Mahn's) to generate the color var, since it sounds like it may not actually be a color anyway. Hopefully this helps.
1
<body style="background-color:<?php echo $color; ?>;">


content


</body>

the style attribute allows you to define custom css to elements, the css attribute, background-color basically defines the color of the background for that element... self explanatory, I know!

Comments

1

Why dont you take a look at these links.

Change background color of a page using php

So instead of doing:

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

Do this:

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

Comments

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

It seems you've made things a little more complicated than it could be. Here's a sample with html integration, as you asked for in the first place. :)

You declare all the elements you need in an array, then you loop 6 times to: generate a random index, take the corresponding element from the array and concatenate it to your color variable.

Finally you echo it anywhere you need in your html file, as said earlier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.