MjM8082 Posted July 20, 2011 Share Posted July 20, 2011 New to php, need help with a for loop on a grid table. Right now I have a table that generates random numbers, but what I need to do is to have this code be able to do this 5 times. Making my table 5x5. Please help! Thanks! This is my code 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 ($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 https://forums.phpfreaks.com/topic/242482-for-loop-need-help/ Share on other sites More sharing options...
MjM8082 Posted July 20, 2011 Author Share Posted July 20, 2011 Also right now the code only generates 1 row of numbers, and I need to display 5 rows and 5 colums of numbers making my table 5X5. This is my assignment exactly from the book... Description: Create a form with a textbox and a Submit button. The textbox can hold a number that will represent the number of rows and columns in a table. Once the submit button is clicked you will generate a grid of num X num rows and columns where num is the integer that was typed in the text box. 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. If Anyone can help me with any of this it would be much needed and much appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245364 Share on other sites More sharing options...
Cagecrawler Posted July 20, 2011 Share Posted July 20, 2011 You just need to put another for loop around the code that generates the row. for($ii = 0; $ii < $number_of_rows; $ii++) { echo "<tr>"; for ($i=0; $i<$number_of_columns; $i++) { $my_no = mt_rand(1, 100); echo "<td>$my_no</td>"; } echo "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245365 Share on other sites More sharing options...
MjM8082 Posted July 20, 2011 Author Share Posted July 20, 2011 Thanks a lot, I couldn't figure out exactly where to put it. Now for the second part of this that I am stuck on is... how do I put the color red behind multiples of 3 and the color blue behind multiples of 2? If you dont exactly know what I mean I posted the description of the assignment above. Quote Link to comment https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245372 Share on other sites More sharing options...
Cagecrawler Posted July 21, 2011 Share Posted July 21, 2011 You can work out if it's a multiple by using the modulus operator (%) and then run an if else to compare different multiples. $my_no = mt_rand(1, 100); if($my_no % 3 == 0) { echo "<td style=\"background-color:red\">$my_no</td>"; } else if($my_no % 2 == 0) { echo "<td style=\"background-color:blue\">$my_no</td>"; } else { echo "<td>$my_no</td>"; } Numbers which are both multiples of 3 and 2 (eg. 6) will be coloured red since that comes first in the if else. Quote Link to comment https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245892 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.