9 Replies - 717 Views - Last Post: 12 March 2014 - 10:38 AM Rate Topic: -----

#1 waheedmohd1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 14

Multiplication table in php using array

Posted 11 March 2014 - 05:25 AM

Hi can any one say , what is wrong in this code?
<?php 
$output='';
if($_POST)
{
	
	$Firstvariable = $_POST['txt1'];
	$secondvariable = $_POST['txt2'];

$output = '<table>';
for($i=0;$i<sizeof($Firstvariable);$i++)
{
	$output.='<tr>';
	 for($j=0;$j<$secondvariable;$j++)
	{
		$output.= '<td>'.($i*$j).'</td>';
	}
$output.='<tr>';	
}
$output.='</table>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<div style="border-style:inset;border-bottom-style:solid;">
<h3>Time table generator</h3>
<fieldset style="padding:60px;">
<legend>
Enter Two Numbers
</legend>
<b>Number 1 :</b> <input type="text" name="txt1" />
<b>Number 2 :</b> <input type="text" name="txt2" /><br />
<p style="padding-left:150px;"><input type="button" name="but1" value="Generate Table" /></p>
</fieldset></div>
</div></form>
<?php
echo $output;
?>
</body>
</html>

This post has been edited by JackOfAllTrades: 11 March 2014 - 06:07 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication table in php using array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Multiplication table in php using array

Posted 11 March 2014 - 06:12 AM

When posting code, USE CODE TAGS!!!

:code:

Now, tell us what is the problem with your code; don't make us guess.
Was This Post Helpful? 0
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: Multiplication table in php using array

Posted 11 March 2014 - 06:40 AM

the problem is in your button!

don't use an <input type="button" ......... /> in a form but an <input type="submit" ......... />. Or otherwise define an action, your button has to launch!

<input type="submit" name="but1" value="Generate Table" />

or
<input type="button" name="but1" value="Generate Table" onclick="DoSomething()" />

Was This Post Helpful? 0
  • +
  • -

#4 waheedmohd1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 14

Re: Multiplication table in php using array

Posted 12 March 2014 - 09:39 AM

still problem is not solved. displaying textbox and button on output screen. when i click on button it is not generating the multiplication table like 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 399
  • View blog
  • Posts: 1,455
  • Joined: 15-January 14

Re: Multiplication table in php using array

Posted 12 March 2014 - 10:04 AM

One problem is that you're using the sizeof function. Sizeof is an alias of count, and the count function returns the number of elements in an array or the number of properties in an object. The variable you are checking, $Firstvariable, is neither an array nor an object.

If you're trying to check the length of a string to see how many characters are in the string, you can use the strlen function:

http://www.php.net/m...tion.strlen.php

It looks like you're just trying to use 2 numbers as input and build the multiplication table based on those numbers. So, since you are expecting numbers there, you should convert them using intval or floatval and then just use them as the loop terminator instead of using a function like sizeof, count, or strlen.

$Firstvariable = intval($_POST['txt1']);

http://www.php.net/m...tion.intval.php
Was This Post Helpful? 0
  • +
  • -

#6 waheedmohd1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 14

Re: Multiplication table in php using array

Posted 12 March 2014 - 10:09 AM

yeah my task is to enter no of rows and column. depending on the row value and column value it should generate the table
Was This Post Helpful? 0
  • +
  • -

#7 waheedmohd1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 14

Re: Multiplication table in php using array

Posted 12 March 2014 - 10:27 AM

Thanks mate problem solved

0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9

but why im getting 1st row as zeros

got it initially $i and $j are equal to 0. so it is getting 0
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 399
  • View blog
  • Posts: 1,455
  • Joined: 15-January 14

Re: Multiplication table in php using array

Posted 12 March 2014 - 10:29 AM

The first row and first column will be 0 because one of the operands will be 0.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9260
  • View blog
  • Posts: 34,748
  • Joined: 12-June 08

Re: Multiplication table in php using array

Posted 12 March 2014 - 10:36 AM

Good ol' Zero times a number..
Was This Post Helpful? 0
  • +
  • -

#10 waheedmohd1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 14

Re: Multiplication table in php using array

Posted 12 March 2014 - 10:38 AM

Final Successful output code
<?php 
$output='';
if($_POST)
{
	$Firstvariable = intval($_POST['txt1']);
	$secondvariable = intval($_POST['txt2']);

$output = '<table>';
for($i=1;$i<($Firstvariable)+1;$i++)
{
	$output.='<tr>';
	 for($j=1;$j<($secondvariable)+1;$j++)
	{
		$output.= '<td>'.($i*$j).'</td>';
	}
$output.='<tr>';	
}
$output.='</table>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<div style="border-style:inset;border-bottom-style:solid;">
<h3>Time table generator</h3>
<fieldset style="padding:60px;">
<legend>
Enter Two Numbers
</legend>
<b>Number 1 :</b> <input type="text" name="txt1" />
<b>Number 2 :</b> <input type="text" name="txt2" /><br />
<p style="padding-left:150px;"><input type="submit" name="but1" value="Generate Table" /></p>
</fieldset></div>
</div></form>
<?php
echo $output;
?>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1