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:
<?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:
$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.
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.