MjM8082 Posted July 20, 2011 Share Posted July 20, 2011 Need help with this php homework assignment, figured this would be the best site to come to. I can't figure out how to do the colors behind the numbers... This is the assignment Each of the cells in the table will hold a random number between 1 and 100 (hint: look at mt_rand() function). If the number is a multiple of 3, display the cell with a red background color; if not a multiple of 3 but a multiple of 2, display the cell with a blue background color. Finally, display the average for all cells. This is the code I have so far... <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Week 1 Lab</title> </head> <body> <?php if (isset($_POST['btn_submit'])) { $number_of_rows = $_POST['number_of_rows']; echo "<table cellpadding='5' cellspacing='5' border='1'"; echo "<tr>"; for($ii = 0; $ii < $number_of_rows; $ii++) { echo "<tr>"; for ($i=0; $i<$number_of_rows; $i++) { $my_no = mt_rand(1, 100); echo "<td>$my_no</td>"; } echo "</tr>"; } echo "</table>"; } ?> <form name="lab1" method="post" action="lab1.php"> <input type="textbox" name="number_of_rows" value="5" /> <input type="submit" name="btn_submit" value="Generate Grid" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Maknib Posted July 20, 2011 Share Posted July 20, 2011 you will want to use the % operator i'm guessing if ( % 3 == 0){ make red } else if( % 3 !=0 && %2 ==0){ make blue } Quote Link to comment Share on other sites More sharing options...
premiso Posted July 20, 2011 Share Posted July 20, 2011 Impressed that someone admits to homework and actually tried it. Kudos. The answer you are looking for lies in the modulus operator. (The % sign). Example: if ($count % 3 == 0) { echo 'multiple of 3'; }elseif ($count % 2 == 0) { echo 'multiple of 2'; } Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 20, 2011 Author Share Posted July 20, 2011 Thanks for the replies guys... Still not quite sure where to place the code you gave me into my code? Also how would I code it so that the red and blue cells will show up? Sorry, still fairly new to php. Quote Link to comment Share on other sites More sharing options...
Maknib Posted July 21, 2011 Share Posted July 21, 2011 don't quote me here but maybe something like this. for ($i=0; $i<$number_of_rows; $i++) { $my_no = mt_rand(1, 100); if ($count % 3 == 0) { $color = "red"; }elseif ($count % 2 == 0) { $color = "blue"; } echo "<td style='background:" . $color . " ;'>$my_no</td>"; } Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 21, 2011 Author Share Posted July 21, 2011 Thanks for the reply. The code you gave me works for the red but doesnt seem to be working with the blue. Not sure what the problem is. I'm still working on it. If you can help, I would appreciate it! Thanks! Quote Link to comment Share on other sites More sharing options...
Maknib Posted July 21, 2011 Share Posted July 21, 2011 Thanks for the reply. The code you gave me works for the red but doesnt seem to be working with the blue. Not sure what the problem is. I'm still working on it. If you can help, I would appreciate it! Thanks! post that code you have now Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 21, 2011 Author Share Posted July 21, 2011 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Week 1 Lab</title> </head> <body> <?php if (isset($_POST['btn_submit'])) { $number_of_rows = $_POST['number_of_rows']; echo "<table cellpadding='5' cellspacing='5' border='1'"; echo "<tr>"; for($ii = 0; $ii < $number_of_rows; $ii++) { echo "<tr>"; for ($i=0; $i<$number_of_rows; $i++) { $my_no = mt_rand(1, 100); echo "<td>$my_no</td>"; if (($my_no%2==0)&&($my_no%3==0)) { $color="#FF00FF"; } else if ($my_no%2==0) { $color="#0000FF"; } else if ($my_no%3==0) { $color="#FF0000"; } else { $color ="#FFFFFF"; } echo "<td style=\"background: {$color};\">$my_no</td>"; } echo "</tr>"; } echo "</table>"; } ?> <form name="lab1" method="post" action="lab1.php"> <input type="textbox" name="number_of_rows" value="5" /> <input type="submit" name="btn_submit" value="Generate Grid" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ignace Posted July 21, 2011 Share Posted July 21, 2011 Each of the cells in the table will hold a random number between 1 and 100 (hint: look at mt_rand() function). If the number is a multiple of 3, display the cell with a red background color; if not a multiple of 3 but a multiple of 2, display the cell with a blue background color. Finally, display the average for all cells. <table> <tr> <?php for ($i = 0, $total = 0, $number = mt_rand(1,100); $i++ < 100/*cols*/; $number = mt_rand(1,100), $total += $number): ?> <td style="background:<?php print !($number % 3) ? '#C00' : (!($number % 2) ? '#0C0' : '#FFF'); ?>"><?php print $number; ?></td> <?php endfor; ?> </tr> </table> Average: <?php print $total / 100/*cols*/; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 21, 2011 Share Posted July 21, 2011 Each of the cells in the table will hold a random number between 1 and 100 (hint: look at mt_rand() function). If the number is a multiple of 3, display the cell with a red background color; if not a multiple of 3 but a multiple of 2, display the cell with a blue background color. Finally, display the average for all cells. <table> <tr> <?php for ($i = 0, $total = 0, $number = mt_rand(1,100); $i++ < 100/*cols*/; $number = mt_rand(1,100), $total += $number): ?> <td style="background:<?php print !($number % 3) ? '#C00' : (!($number % 2) ? '#0C0' : '#FFF'); ?>"><?php print $number; ?></td> <?php endfor; ?> </tr> </table> Average: <?php print $total / 100/*cols*/; ?> Quit showing off, LOL. At least give the guy some comments I'm with Permiso, I'm surprised we have someone actually admitting they have a homework problem and they actually attempted it before posting. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 22, 2011 Share Posted July 22, 2011 At least give the guy some comments I did in my editing session but my internet connection went dead and didn't come back on until now (after they replaced the modem). So here goes: @OP: <table> <tr> <?php for ($i = 0, $total = 0, $number = mt_rand(1,100); $i++ < 100/*cols*/; $total += $number, $number = mt_rand(1,100)): ?> <td style="background:<?php print !($number % 3) ? '#C00' : (!($number % 2) ? '#0C0' : '#FFF'); ?>"><?php print $number; ?></td> <?php endfor; ?> </tr> </table> Average: <?php print $total / 100/*cols*/; ?> A more expanded example would be: <table> <tr> <?php $total = 0; $number = mt_rand(1,100); // get a starting random number for ($i = 0; $i < 100/*number of cells you want to generate*/; ++$i) { $color = '#FFF'; // default color if (($number % 3) == 0) { // same as !($number % 3) $color = '#C00'; // red } else if (($number % 2) == 0) { $color = '#0C0'; // blue } print '<td style="background:' . $color . '">' . $number . '</td>'; $total += $number; $number = mt_rand(1,100); // new random number to feed the next iteration } ?> </tr> <caption>Average: <?php print $total / 100; ?></caption> </table> This will create 100 columns, if you want to create a specific set of rows/cols you should amend the code. The above examples should give you a good understanding of how you can generate the different cells with the numbers and calculate the average at the end. Quote Link to comment 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.