1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Random background colour code.

Discussion in 'PHP' started by sinkquick, Aug 29, 2006.

  1. #1
    Hey guys. I'm a first poster here. I was just wondering if you guys could possibly help me out with my code?

    Here's the code and a description of what it's supposed to do:

    
    <?php
    	$RandomColourScheme=rand(0,6);
    	if($RandomColourScheme=0){
    		$ColourScheme = "#CCCCFF";
    	}
    	elseif($RandomColourScheme=1){
    		$ColourScheme = "#CCFFFF";
    	}
    	elseif($RandomColourScheme=2){
    		$ColourScheme = "#99FF00";
    	}
    	elseif($RandomColourScheme=3){
    		$ColourScheme = "#CCFF00";
    	}
    	elseif($RandomColourScheme=4){
    		$ColourScheme = "#CCCCCC";
    	}
    	elseif($RandomColourScheme=5){
    		$ColourScheme = "#FFFF99";
    	}
    	elseif($RandomColourScheme=6){
    		$ColourScheme = "#9966FF";
    	}
    ?>
    
    PHP:
    Okay, the code is supposed to generate a random number between 0 and 6. The if statement is supposed to work out which number has been generated and applies a colour to the $ColourScheme varible.

    The HTML looks something like this:

    <td bgcolor="<?php print ("$ColourScheme"); ?>">

    Any ideas of why it's not working?

    Kind regards, James.
     
    sinkquick, Aug 29, 2006 IP
  2. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Are you using the ".php" extension on the file? If you are not you should change it.

    If that doesn't work try using:

    <td bgcolor="<?php echo $ColourScheme;?>">
     
    danielbruzual, Aug 29, 2006 IP
  3. sinkquick

    sinkquick Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, the ".php" extension is there. Thanks for the help but the echo doesn't work. I know the code works because I had this as my old code.

    
    <?php
        	if(date("D") == "Sun")
        	{
        	$ColourScheme = "#CCCCCC";
        	}
        	elseif(date("D") == "Mon")
        	{
        	$ColourScheme = "#A6CAF0";
        	}
        	elseif(date("D") == "Tue")
        	{
        	$ColourScheme = "#E79703";
        	}
        	elseif(date("D") == "Wed")
        	{
        	$ColourScheme = "#AE9675";
        	}
        	elseif(date("D") == "Thu")
        	{
        	$ColourScheme = "#CFDDF4";
        	}
        	elseif(date("D") == "Fri")
        	{
        	$ColourScheme = "#CCCCCC";
        	}
        	elseif(date("D") == "Sat")
        	{
        	$ColourScheme = "#A6CAF0";
        	}
    ?>
    
    PHP:
    This one works fine. This one changes the colour depending on the day of the week.
     
    sinkquick, Aug 29, 2006 IP
  4. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When comparing a variabile with a number or another variable, you have to use == :

    
    
    if ($value == 1) 
    { //do something }
    elseif ($secondvalue==2)
     {
     }
    
    PHP:
     
    mariush, Aug 29, 2006 IP
    danielbruzual likes this.
  5. sinkquick

    sinkquick Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ahh, that's right! I totally forgot! Thank you so much! :D
     
    sinkquick, Aug 29, 2006 IP
  6. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #6
    nice catch, I had not noticed!
     
    danielbruzual, Aug 29, 2006 IP