Jump to content

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
https://forums.phpfreaks.com/topic/242486-need-help-with-php-homework/
Share on other sites

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

 

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

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

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

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.

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.

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.