Jump to content

Recommended Posts

New to php, need help with a for loop on a grid table.

Right now I have a table that generates random numbers, but what I need to do is to have this code be able

to do this 5 times. Making my table 5x5. Please help! Thanks!

 

This is my code 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 ($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/242482-for-loop-need-help/
Share on other sites

Also right now the code only generates 1 row of numbers, and I need to display 5 rows and 5 colums of numbers making my table 5X5.

 

This is my assignment exactly from the book...

 

Description: Create a form with a textbox and a Submit button. The textbox can hold a number that will represent the number of rows and columns in a table. Once the submit button is clicked you will generate a grid of num X num rows and columns where num is the integer that was typed in the text box. 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.

 

 

If Anyone can help me with any of this it would be much needed and much appreciated.  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245364
Share on other sites

Thanks a lot, I couldn't figure out exactly where to put it. Now for the second part of this that I am stuck on is... how do I put the color red behind multiples of 3 and the color blue behind multiples of 2? If you dont exactly know what I mean I posted the description of the assignment above.

Link to comment
https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245372
Share on other sites

You can work out if it's a multiple by using the modulus operator (%) and then run an if else to compare different multiples.

$my_no = mt_rand(1, 100);
if($my_no % 3 == 0)
{
    echo "<td style=\"background-color:red\">$my_no</td>";
}
else if($my_no % 2 == 0)
{
    echo "<td style=\"background-color:blue\">$my_no</td>";
}
else
{
    echo "<td>$my_no</td>";
}

Numbers which are both multiples of 3 and 2 (eg. 6) will be coloured red since that comes first in the if else.

Link to comment
https://forums.phpfreaks.com/topic/242482-for-loop-need-help/#findComment-1245892
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.