I want to make a php program to print multiplication tables for integers from 1 to10;Result should be displayed using a 10x10 HTML table.
I tried the following code which is not working.
<html><head><title>Multiplication Table</title></head>
<body>
<?php
for($a=1; $a<=10; $a++){
for($b=1; $b<=10; $b++){
$r=$a * $b;
}
}
?>
<table border="1">
<tr>
<th>A * B</th>
<th>Result</th>
</tr>
<?php
$i=1;
while ($i<=100){ ?>
<tr><td>echo "$a*$b"</td><td> echo "$r"</td> </tr> <br>
<?php
$i++;
}
?>
</table>
</body>
</html>
Please help.
Thnx in advance.
markspark100 11-18-2011, 10:45 AM This should work for you.
<?php
echo '<table>';
for($a=1; $a<=10; $a++)
{
echo '<tr>';
for($b=1; $b<=10; $b++)
{
echo '<td>'.$a*$b.'</td>';
}
echo '</tr>';
}
echo '<table>';
?>
Mark
No I tried your code as follows but it didn't work!! :eek:
<html><head><title>Multiplication Table</title></head>
<body>
<?php
echo '<table>';
for($a=1; $a<=10; $a++)
{
echo '<tr>';
for($b=1; $b<=10; $b++)
{
echo '<td>'.$a*$b.'</td>';
}
echo '</tr>';
}
echo '<table>';
?>
</body>
</html>
markspark100 11-18-2011, 11:06 AM What output do you want then?
I get the following:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Is that not what you want?
Mark
Ya of course I want the above output@Mark.
But when I run the program it gives error in line 15!! :eek:
Can you please post the full program for me which works for you?
Or else please check the code above which I have posted after considering your corrections but it is not working
timgolding 11-18-2011, 12:26 PM No I tried your code as follows but it didn't work!! :eek:
<html><head><title>Multiplication Table</title></head>
<body>
<?php
echo '<table>';
for($a=1; $a<=10; $a++)
{
echo '<tr>';
for($b=1; $b<=10; $b++)
{
echo '<td>'.$a*$b.'</td>';
}
echo '</tr>';
}
echo '<table>';
?>
</body>
</html>
I can see the table closing tag is not a closing tag should be change to </table>
Other than that looks fine what did the error say?
I just tested and it worked perfectly for me :/
markspark100 11-18-2011, 12:27 PM What I originally posted was exactly what worked for me.
I have also tried your code and that still works see the attached image.
What error does it give you?
Mark
markspark100 11-18-2011, 12:29 PM I can see the table closing tag is not a closing tag should be change to </table>
Other than that looks fine what did the error say?
Oops good spot...my bad.
I am getting the following error-
Parse error: syntax error, unexpected '<' in D:\xampp\htdocs\test\mul10.php on line 15
Fou-Lu 11-18-2011, 05:54 PM I am getting the following error-
Parse error: syntax error, unexpected '<' in D:\xampp\htdocs\test\mul10.php on line 15
From this screenshot, there is no error. It also parses in the browser from what we can see. The difference is that the code displayed in the browser appears to be index.php while the error is on mul10.php. Which is in use?
Copy and paste the code from the editor into the forums. Its easier to find error in text than it is in an image.
markspark100 11-18-2011, 10:55 PM From this screenshot, there is no error. It also parses in the browser from what we can see. The difference is that the code displayed in the browser appears to be index.php while the error is on mul10.php. Which is in use?
Copy and paste the code from the editor into the forums. Its easier to find error in text than it is in an image.
That was my screenshot showing that the code I posted does work for me, not ippo's who says it doesn't work for him.
Mark
Ok now it works. I created a new program
Thanks
But now what I want to do is display 1,2,3,...10 beside and above each row and column head.
I tried this but it is not showing the desired output-
<html><head><title>Multiplication Table</title></head>
<body>
<?php
echo '<table>'; echo '<tr>';
echo '<td> </td>';
for($i=1;$i<=10;$i++)
{
echo '<td> '.$i.'</td>';
}echo '</tr>';
echo '</table>';
echo '<table>';
for($i=1;$i<=10;$i++)
{
echo '<tr>';
echo '<td> '.$i.'</td>';
echo '</tr>';
}
echo '</table>';
echo '<table border="1" cellspacing=0>';
for($a=1; $a<=10; $a++)
{
echo "<tr>";
for($b=1; $b<=10; $b++)
{
echo '<td>'.$a*$b.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>
How to do that?
Can you help further in this regard?
Thanks in advance
markspark100 11-19-2011, 08:32 AM <html><head><title>Multiplication Table</title></head>
<body>
<?php
echo '<table>';
echo '<tr>';
for($i=1; $i<=10;$i++)
echo '<td>'.$i.'</td>';
echo '</tr>';
for($a=1; $a<=10; $a++)
{
echo '<tr>
<td>$a</td>';
for($b=1; $b<=10; $b++)
{
echo '<td>'.$a*$b.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>
Fou-Lu 11-19-2011, 05:30 PM That was my screenshot showing that the code I posted does work for me, not ippo's who says it doesn't work for him.
Mark
Oops my bad.
<html><head><title>Multiplication Table</title></head>
<body>
<?php
echo '<table>';
echo '<tr>';
for($i=1; $i<=10;$i++)
echo '<td>'.$i.'</td>';
echo '</tr>';
for($a=1; $a<=10; $a++)
{
echo '<tr>
<td>$a</td>';
for($b=1; $b<=10; $b++)
{
echo '<td>'.$a*$b.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>
This still doesn't look quite right. This will provide an actual variable name alongside the rows since there is no parse. Removing it though will loose track of the 'blank' position in the table (where no numbers are multiplied against), but could be handled with 0.
I'd suggest just making use of the 0th position in the loop for just that. Some simple calculation on what position the row/col are and you can make a table. I'll add a quick inline style here so its easier to see:
$iRows = 10;
$iCols = 10;
print '<table>';
for ($i = 0; $i <= $iRows; ++$i)
{
print '<tr>';
for ($j = 0; $j <= $iCols; ++$j)
{
// Get the display as a string
$sOutput = ($i == 0 && $j == 0) ? '' : ($i == 0 ? $j : ($j == 0 ? $i : $i * $j));
// Lets write a quick inline style for this so its easier to show the multipliers
$sStyle = '';
$sStyle .= $i == 0 ? ' border-bottom: 1px solid black;' : '';
$sStyle .= $j == 0 ? ' border-right: 1px solid black;' : '';
// Print it.
printf('<td style="%s">%s</td>' . PHP_EOL, $sStyle, $sOutput);
}
print '</tr>';
}
print '</table>';
That's how I perceive how the table should look.
|