James0231 Posted September 25, 2015 Share Posted September 25, 2015 Hey guys I'm brand new to php and I'm trying to make script that will generate a 10x10 grid of randomly generated colors that display the RGB color code inside the grid in both white and black. So far I have a 3x3 grid of the HTML that the PHP script would have to generate in order to accomplish the grid but I'm not sure about how to do the whole thing. Any help? below it's what I have so far (it's all the same color though) <!DOCTYPE html> <html> <head> </head> <body> <table> <tbody> <tr> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> </tr> <tr> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> </tr> <tr> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> <td style="background-color: #a3bc34;">A3BC34<br /><span style="color: #ffffff;">A3BC34</spam></td> </tr> </tbody> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298303-color-grid/ Share on other sites More sharing options...
hansford Posted September 25, 2015 Share Posted September 25, 2015 (edited) Here is one way of accomplishing the task. You don't need the two arrays as you could just have an array of 2 values and randomly select a 0 or 1 for each square. <!doctype html> <html> <head> <style type="text/css"> .white { width:50px; background-color: #ffffff; } .black { width:50px; height:50px; background-color: #000000; } </style> </head> <body> <table border=1> <?php $white = array_fill(0,10,'white'); $black = array_fill(0,10,'black'); $colors = array_merge($white,$black); shuffle($colors); //$colors = array('white','black'); for($i = 0; $i < 10; $i++) { echo "<tr>"; for($j = 0; $j < 10; $j++) { echo "<td class='" . $colors[rand(0,19)] . "'></td>"; } echo "</tr>"; } ?> </table> </body> Edited September 25, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/298303-color-grid/#findComment-1521511 Share on other sites More sharing options...
James0231 Posted September 25, 2015 Author Share Posted September 25, 2015 (edited) Thank you. Also, I think you misunderstood the part about the RGB black and white colors. The colors of the grid can be random (many different colors) while the RGB color code is to be displayed inside the grid in both white and black. I'm having trouble with that part. What I'm trying to accomplish is something like this but in a 10x10 grid (see attachment) Edited September 25, 2015 by James0231 Quote Link to comment https://forums.phpfreaks.com/topic/298303-color-grid/#findComment-1521513 Share on other sites More sharing options...
hansford Posted September 25, 2015 Share Posted September 25, 2015 Make changes as needed, but this will give you what you indicated. <!doctype html> <html> <head> <style type="text/css"> .white-text { display:block; color: #ffffff; } .black-text { display:block; color: #000000; } .white { width:50px; height:50px; background-color: #ffffff; } .black { width:50px; height:50px; background-color: #173534; } .green { width:50px; height:50px; background-color: #247422; } .pee-green { width:50px; height:50px; background-color: #A3BC34; } .gray { width:50px; height:50px; background-color: #AABBCC; } .dark-blue { width:50px; height:50px; background-color: #1351AC; } .lt-green { width:50px; height:50px; background-color: #547142; } .red { width:50px; height:50px; background-color: #AD2431; } .teal { width:50px; height:50px; background-color: #113344; } .purple { width:50px; height:50px; background-color: #431241; } </style> </head> <body> <table border=1> <?php $colors = array(array('white','FFFFFF'),array('black','173534'), array('green','247422'),array('pee-green','A3BC34'),array('gray','AABBCC'), array('dark-blue','1351AC'),array('lt-green','547142'),array('red','AD2431'), array('teal','113344'),array('purple','431241')); for($i = 0; $i < 10; $i++) { echo "<tr>"; for($j = 0; $j < 10; $j++) { $rand = rand(0,9); echo "<td class='" . $colors[$rand][0] . "'> <span class='black-text'>{$colors[$rand][1]}</span> <span class='white-text'>{$colors[$rand][1]}</span> </td>"; } echo "</tr>"; } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298303-color-grid/#findComment-1521514 Share on other sites More sharing options...
Barand Posted September 25, 2015 Share Posted September 25, 2015 or <?php function randColor() { $r = rand(0,255); $g = rand(0,255); $b = rand(0,255); return sprintf('%02X%02X%02X', $r, $g, $b); } $tdata=''; for ($i=0; $i<10; $i++) { $tdata .= "<tr>"; for ($j=0; $j<10; $j++) { $c = randColor(); $tdata .= "<td style='background-color:#$c'>$c<br><span class='wtext'>$c</span></td>"; } $tdata .= '</tr>'; } ?> <html> <head> <style type='text/css'> table { border-collapse: collapse; } td { width: 60px; height: 60px; font-family: sans-serif; font-size: 8pt; text-align: center; } .wtext { color: white; } </style> </head> <body> <table border='1'> <?=$tdata?> </table> </body> </html> example output Quote Link to comment https://forums.phpfreaks.com/topic/298303-color-grid/#findComment-1521518 Share on other sites More sharing options...
James0231 Posted September 25, 2015 Author Share Posted September 25, 2015 Thank you. I'll keep studying the code. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/298303-color-grid/#findComment-1521524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.