Jump to content

Recommended Posts

I am trying to write a code that generates a grid table 5x5, 10X10, 12x12, whichever number the user types it will display a table. Each cell on the table will display a random number. Multiples of 3 will have the color red behind it and Multiples of 2 will have blue behind it.

 

Everything was working great, but now I cant seem to get the table to work correctly. My code is duplicating the table and i'm not sure why. I think my for loops might not be closed correctly. If anyone can help I would appreciate it. Thanks.

 

 

<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
https://forums.phpfreaks.com/topic/242581-php-code-help/
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.