Multiplication Table in PHP taking Number and Limit from another Form and CSS Styling

Here the user will enter the number and the limit upto which he wants the table, based on that the Multiplication Table of that number gets generated.

We need to create a HTML Form where the user will enter the values.

MultiplicationTableForm.html

<html>
<head>
<title>Multiplication Table Generator Form</title>

<style type="text/css">
table.tab
	{
	border: 1px solid blue;
	text-align: center;
	border-collapse:collapse;
	background: #E0FFFF;
   }

tr.head{font-family: Tahoma,Arial; font-size: 15pt; font-style: normal; font-weight:; color: white;
text-align: center; background-color:#00CED1}

tr.bod{font-family:Tahoma, Arial; font-size: 11pt; font-style: normal; font-weight: ; color:black}

input.sub{font-family:Tahoma, Arial; font-size: 12pt; font-style: normal; font-weight: ; color:black}
</style>

</head>
<body>
<br /><br />
<form name="MultiTableForm" method="POST" action="MultiTable.php">

	<table border='0' width='400' cellspacing='10' cellpadding='05' align='center' class='tab'>
		<tr class='head'>
			<td colspan='2' align='center'>Multiplication Table Generator<br /><br /></td>
		</tr>

		<tr class='bod'>
			<td>Number:</td>
			<td><input type="text" name="num" id="num" />1-100</td>
		</tr>

		<tr class='bod'>
			<td>Upto:</td>
			<td><input type="text" name="upto" id="upto" />1-100</td>
		</tr>

		<tr>
			<td colspan='2' align='center'><br /><br />
			<input type="submit" name="submit" value="Submit" class='sub' />&nbsp;&nbsp;
			<input type="reset" name="reset" value="Reset Fields" class='sub' />
			</td>
		</tr>
	</table>
</form>

</body>
</html>

Snapshot of the Form

MultiTableHTMLForm1

The PHP File will take the values and generate the Form. Appropriate validations are performed here.

MultTable.php

<html>
<head>
<title>Multiplication Table</title>

<style type="text/css">
	/*Style for Table*/
	table
	{
	border-collapse:collapse; width:0%;
	}

	td
	{
	height:35px;
	}
	/*Style for Table Header*/
	td.head{font-family: Tahoma; font-size: 15pt; font-style: normal; color: white;
    text-align: center; background: #00CED1;}

	/*Style of other Table Elements*/
	table, th, td
	{
	font-family:Tahoma, Arial; font-size:11pt;
	color:black; font-style: normal; font-weight: normal;
	border: 1px solid blue;
	text-align: center;
	}

</style>

</head>

<body>

<?php

$num = $_POST['num'];
$upto = $_POST['upto'];

$errorFlag = 0;
$errorString = "Invalid Arguments: <br />";

if ($num <1 || $num >100) {
		$errorFlag = 1;
		$errorString = $errorString." - Number should be between 1 to 100 <br />";
	}
	if ($upto < 1 || $upto > 100) {
		$errorFlag = 1;
		$errorString = $errorString." - Limit should be between 1 to 100";
	}

if ($errorFlag == 0) {

echo "<table align='center'>";

echo "<tr>";
echo "<td colspan='5' class='head'>
		Multiplication Table of $num upto $upto
	</td>";
echo "<tr>";

for($i=1; $i<=$upto; $i++)
{
	if($i%2 == 0)
	{
		echo "<tr bgcolor='PowderBlue'>";
	}

	else
	{
		echo "<tr bgcolor='LightCyan'>";
	}

	echo "<td>$num</td>";

	echo "<td>x</td>";

	echo "<td>$i</td>";

	echo "<td>=</td>";

	echo "<td>".$num*$i."</td>";

	echo "</tr>";

}//for loop ends

echo "</table>";
}

else
{
  echo "<div align='center'>";
  echo "$errorString";
  echo "<br /><br /><br />";
  echo "<a href='MultiTableForm.html'>Back</a>";
  echo "</div>";
}
?>

</body>

</html>

Snapshot of the o/p

User enters the number and limit upto which he wants a Table

MultiTableHTMLForm

Multiplication Table gets generated.

MultiTableGenerated

So Here it comes. Very Useful Program for any School kid. :D and a great learning tool for PHP Beginners. :D

Multiplication Table in Java

This is a very basic program But for a beginner it is worth trying.

And it also makes us aware about the power of for loop which can repeat so many times within a second.

public class MulTable
{
	public static void main(String[] args)
	{
		int i;
		int num = 9;
		for(i=1; i<=10; i++)
		{
			System.out.println(num+ " * " +i+ " = "+num * i);
		}
	}
}

Snapshot of the o/p

PHP Multiplication Table with CSS

Here comes my first PHP program at Programming Palace. Learn and Enjoy!!! For all newbies to the world of programming and PHP. Try it for some time. You will fell in love with it. :D

<html>

<head>

<title>Multiplication Table in PHP</title>

<style type="text/css">
	/*Style for Table*/
	table
	{
	border-collapse:collapse; width:25%;
	}

	td
	{
	height:35px;
	}

	table, th, td
	{
	font-family:Arial; font-size:16px;

	color:navy; font-weight: bold;

	border: 2px solid blue;

	text-align: center;
	}
	/*Style for The Heading*/

	h2
	{
	font-family:Arial; font-size:20px;

	color: orange; font-weight: bold; text-align: center;

	text-decoration:underline
	}

</style>

</head>

<body>

<?php

$num=25;

echo "<table align='center'>";

echo "<tr>";

echo "<td colspan='5' bgcolor='#E0FFFF'>
		<h2>Multiplication Table of 25</h2></td>";

echo "<tr>";

for($i=1; $i<=20; $i++)
{
	if($i%2 == 0)
	{
		echo "<tr bgcolor='#E0FFFF'>";
	}

	else
	{
		echo "<tr bgcolor='#ADD8E6'>";
	}

	echo "<td>$num</td>";

	echo "<td>x</td>";

	echo "<td>$i</td>";

	echo "<td>=</td>";

	echo "<td>".$num*$i."</td>";

	echo "</tr>";

}//for loop ends

echo "</table>";

?>

</body>

</html>

Snapshot of the o/p