Easy:PHP & Random

Discussion in 'PHP' started by Dirty-Rockstar, Feb 19, 2007.

  1. #1
    Hey, I'm trying to figure out how i can make it so random has more then 2 choices. below is code so when a page is refreshed the block will either be black or white. what i want is black,white,blue,red ETC. the random function only allows 2 variables. what am i missing here. switches maybe? i keep getting nutty errors when i try to accomplish it. and i believe there is a way shorter way to accomplish this

    your support is great for the noob coders like myself

    Thank you in advance

    <?
    $black="bgcolor='black'";
    $white="bgcolor='white'";
    $name=1;
    $name2=2;
    $random=rand($name,$name2);
    
    if($random==1)
    {
    
    Print"
    
    <TABLE BORDER=1>
    <TR>
      <TD $black width='25' height='25' value='value' name='name'></TD>
     
    </TR>
    
    </TABLE>";
    
    }else{
    
    Print"
    
    <TABLE BORDER=1>
    <TR>
      <TD $white width='25' height='25' value='value' name='name'></TD>
     
    
    </TABLE>";
    
    }
    ?>
    PHP:

     
    Dirty-Rockstar, Feb 19, 2007 IP
  2. vinodkv

    vinodkv Peon

    Messages:
    183
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    $black="bgcolor='black'";
    $white="bgcolor='white'";
    $blue = "bgcolor='blue'";
    $random=rand(1,3);
    
    if($random==1)
    {
    Print"<TABLE BORDER=1><TR>  <TD $black width='25' height='25' value='value' name='name'></TD> </TR></TABLE>";
    }
    elseif($random==2)
    {
    Print"<TABLE BORDER=1><TR>  <TD $white width='25' height='25' value='value' name='name'></TD> </TABLE>";}
    }
    else
    {
    Print"<TABLE BORDER=1><TR>  <TD $blue width='25' height='25' value='value' name='name'></TD> </TABLE>";
    }
    ?>
    PHP:
     
    vinodkv, Feb 19, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    
    $bgcolors = array('black', 'white', 'red', 'green');
    
    $bgcolor = $bgcolors[array_rand($bgcolors)];
    
    echo '<TABLE BORDER="1">
    <TR>
      <TD style="background-color: '. $bgcolor .';" width="25" height="25"> <input value="value" name="name"></TD>
     
    </TR>
    
    </TABLE>';
    
    PHP:
    Your HTML looks slightly broken too. I fixed it a little bit.
     
    nico_swd, Feb 19, 2007 IP
  4. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for the support, im not too farmiliar with arrays yet, got the if statement down, and now i know switches which are super cool. thanks for fixing my html as well, thats not a huge deal at the moment. these are all rush jobs to fixure out php. its getting easier per day.

    af far as the first poster thank you for showing me in a way i had it. it both posts gives me somthing to work with and now im forced to learn arrays LOL

    WEEE. php is cool. lovin it.
     
    Dirty-Rockstar, Feb 19, 2007 IP