Jump to content

Need Help With PHP Code


MjM8082

Recommended Posts

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>

 

Link to comment
Share on other sites

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;

 

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.