MjM8082 Posted July 25, 2011 Share Posted July 25, 2011 I need help writing a code that will calculate AND display the average of my table. I've been looking through books and online forums but can't seem to find out how to do this. I'm new to PHP also. Figured this would be the best site to come to. 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='10' cellspacing='10' 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); 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 https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/ Share on other sites More sharing options...
Nodral Posted July 25, 2011 Share Posted July 25, 2011 Hi Every time your script generates a number, get it to allocate into an array (eg $average_calc), then use the following. $total=array_sum($average_calc); $count=array_count($average_calc); $average=$total/$count; Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246820 Share on other sites More sharing options...
MjM8082 Posted July 25, 2011 Author Share Posted July 25, 2011 How would I put that into my code? Where exactly? Bare with me, still learning how to code. Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246821 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 does your table display the layout that you expect it to? I have doubts about the structure, however to find the average this should work for you.. if (isset($_POST['btn_submit'])) { $number_of_rows = $_POST['number_of_rows']; echo "<table cellpadding='10' cellspacing='10' 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); 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>"; } $total += $my_no; echo "</tr>"; } echo "</table>"; $average = ($total/$number_of_rows); print $average; } Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246822 Share on other sites More sharing options...
Nodral Posted July 25, 2011 Share Posted July 25, 2011 Try this <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='10' cellspacing='10' 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); $average_calc[]=$my_no; 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>"; } $total=array_sum($average_calc); $count=count($average_calc); $average=$total/$count; ?> <form name="lab1" method="post" action=""> <input type="textbox" name="number_of_rows" value="5" /> <input type="submit" name="btn_submit" value="Generate Grid" /> <?php echo $average; ?> </form> </body> </html> I've added in line 16 to create an array Lines 37 - 39 to calculate the average Lines 45 - 47 to print on screen Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246825 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 Try this <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='10' cellspacing='10' 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); $average_calc[]=$my_no; 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>"; } $total=array_sum($average_calc); $count=count($average_calc); $average=$total/$count; ?> <form name="lab1" method="post" action=""> <input type="textbox" name="number_of_rows" value="5" /> <input type="submit" name="btn_submit" value="Generate Grid" /> <?php echo $average; ?> </form> </body> </html> I've added in line 16 to create an array Lines 37 - 39 to calculate the average Lines 45 - 47 to print on screen should work but is more work than needs to be done here Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246826 Share on other sites More sharing options...
marcus Posted July 25, 2011 Share Posted July 25, 2011 No need to put it in an array. $sum = 0; for($i = 0; $i < $countRows; $i++){ $sum += mt_rand(1,100); // other code... } $avg = $sum/$countRows; Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246827 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 No need to put it in an array. $sum = 0; for($i = 0; $i < $countRows; $i++){ $sum += mt_rand(1,100); // other code... } $avg = $sum/$countRows; same code I posted Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246828 Share on other sites More sharing options...
marcus Posted July 25, 2011 Share Posted July 25, 2011 No need to put it in an array. $sum = 0; for($i = 0; $i < $countRows; $i++){ $sum += mt_rand(1,100); // other code... } $avg = $sum/$countRows; same code I posted Your code would throw an undefined variable. Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246831 Share on other sites More sharing options...
Nodral Posted July 25, 2011 Share Posted July 25, 2011 should work but is more work than needs to be done here There's no "should" should about it. It does work lol Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246834 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 No need to put it in an array. $sum = 0; for($i = 0; $i < $countRows; $i++){ $sum += mt_rand(1,100); // other code... } $avg = $sum/$countRows; same code I posted Your code would throw an undefined variable. and where would the undefined variable be? Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246838 Share on other sites More sharing options...
marcus Posted July 25, 2011 Share Posted July 25, 2011 $total is never defined before you try incrementing it. Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246840 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 should work but is more work than needs to be done here There's no "should" should about it. It does work lol right but for someone that is new to PHP, dealing with arrays and array functions might not be the best approach here...it's important that he understands what he is doing instead of simply copy pasting code...but yes your code w :-*ill work too... Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246845 Share on other sites More sharing options...
MjM8082 Posted July 25, 2011 Author Share Posted July 25, 2011 Thanks for the replies everyone, the codes worked great. One more quick question... How would I make it say "Your average is..." in text and then the number next to it? Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246847 Share on other sites More sharing options...
Nodral Posted July 25, 2011 Share Posted July 25, 2011 echo"</br>Your Average is $average"; Don't worry AkKay47, I won't get him to put that in an array!!! lol Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246848 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 $total is never defined before you try incrementing it. sure it is $total += $my_no; right there.. OP print "The average is: $average"; Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246849 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 echo"</br>Your Average is $average" Don't worry AkKay47, I won't get him to put that in an array!!! lol lol...your're too quick with the replies..keep beating me... :'( Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246850 Share on other sites More sharing options...
marcus Posted July 25, 2011 Share Posted July 25, 2011 $total is never defined before you try incrementing it. sure it is $total += $my_no; right there.. OP print "The average is: $average"; I think you should go back to the basics. You're trying to update a variable that has never been defined before. $total needs to be instantiated before you can start throwing things at it. $total = 0; // this would be instantiating it Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246851 Share on other sites More sharing options...
MjM8082 Posted July 25, 2011 Author Share Posted July 25, 2011 Thanks AyKay and Nodral, appreciate it a lot. Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246852 Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 $total is never defined before you try incrementing it. sure it is $total += $my_no; right there.. OP print "The average is: $average"; I think you should go back to the basics. You're trying to update a variable that has never been defined before. $total needs to be instantiated before you can start throwing things at it. $total = 0; // this would be instantiating it right you are, I don't need to go back to basics..human error Quote Link to comment https://forums.phpfreaks.com/topic/242752-need-help-with-php-code/#findComment-1246854 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.