Jump to content

Need help with PHP grid


MjM8082

Recommended Posts

I have a code that generates a table 5x5 with random numbers 1-100 in each cell. What I need to do is, every number that is a multiple of 3 I need that cell to be red and every number that is a multiple of 2 I need that cell to be blue. Appreciate any help at all. This is the code I have right now...

 

 

 

<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/242489-need-help-with-php-grid/
Share on other sites

I didn't just do your homework did I?

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

			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="tableTest.php">
<input type="textbox" name="number_of_rows" value="5" />
<input type="submit" name="btn_submit" value="Generate Grid" />
</form>

</body>
</html>

Lol thank you for your help and reply... there is still a problem with the table though. I tried running the table with the code you gave me... the colors work correctly but now the table is messed up... it duplicates the table twice instead of once. I think the for loop might of not been closed correctly or something. Let me know if you can help, still fairly new to php. Thanks a lot!

Archived

This topic is now archived and is closed to further replies.

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