Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: mul table

  1. #1
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts

    mul table

    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.
    Code:
    <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.

  • #2
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    58
    Thanks
    5
    Thanked 5 Times in 5 Posts
    This should work for you.

    PHP Code:
    <?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

  • Users who have thanked markspark100 for this post:

    ippo (11-19-2011)

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    No I tried your code as follows but it didn't work!!
    Code:
    <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>

  • #4
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    58
    Thanks
    5
    Thanked 5 Times in 5 Posts
    What output do you want then?

    I get the following:

    Code:
    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

  • #5
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Ya of course I want the above output@Mark.
    But when I run the program it gives error in line 15!!
    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

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by ippo View Post
    No I tried your code as follows but it didn't work!!
    Code:
    <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 :/
    Last edited by timgolding; 11-18-2011 at 12:30 PM.
    You can not say you know how to do something, until you can teach it to someone else.

  • Users who have thanked timgolding for this post:

    ippo (11-19-2011)

  • #7
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    58
    Thanks
    5
    Thanked 5 Times in 5 Posts
    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
    Attached Thumbnails Attached Thumbnails mul table-print-screen.png  

  • #8
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    58
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by timgolding View Post
    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.

  • #9
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    I am getting the following error-
    Parse error: syntax error, unexpected '<' in D:\xampp\htdocs\test\mul10.php on line 15

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by ippo View Post
    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.

  • #11
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    58
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Fou-Lu View Post
    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

  • #12
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    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-
    Code:
    <html><head><title>Multiplication Table</title></head>
    
    <body>
    
    <?php
    echo '<table>'; echo '<tr>';
    echo '<td>&nbsp; </td>';
    for($i=1;$i<=10;$i++)
    {
    
      echo '<td>&nbsp; '.$i.'</td>';
     
        }echo '</tr>';
    	echo '</table>';
    	
    	echo '<table>'; 
    for($i=1;$i<=10;$i++)
    {
     echo '<tr>';
      echo '<td>&nbsp; '.$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
    Last edited by ippo; 11-19-2011 at 01:06 AM.

  • #13
    New Coder
    Join Date
    May 2009
    Location
    Birmingham, England
    Posts
    58
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Code:
    <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>
    Last edited by markspark100; 11-19-2011 at 08:40 AM.

  • Users who have thanked markspark100 for this post:

    ippo (11-19-2011)

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by markspark100 View Post
    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.

    Quote Originally Posted by markspark100 View Post
    Code:
    <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:
    PHP Code:
    $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 == && $j == 0) ? '' : ($i == $j : ($j == $i $i $j));
            
            
    // Lets write a quick inline style for this so its easier to show the multipliers
            
    $sStyle '';
            
    $sStyle .= $i == ' border-bottom: 1px solid black;' '';
            
    $sStyle .= $j == ' 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •