Jump to content

Need Help With PHP Homework


MjM8082

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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*/; ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.