Search code examples
phplogic

PHP script that creates the following table (use for loops)


In one of my project I want to have pattern of certain number as depicted in image but its little chnaged, Any help will be appreciated

<?php  
    echo "<table border =\"1\" style='border-collapse: collapse'>";  
    for ($row=1; $row <= 10; $row++) {   
        echo "<tr> \n";  
        for ($col=1; $col <= 10; $col++) {   
            $p = $col+1 * $row+1;  
            echo "<td>$p</td> \n";  
        }  
        echo "</tr>";  
    }  
    echo "</table>";  
?>  

enter image description here

Expected Output

enter image description here


Solution

  • try this, remove increment in your $col and $row

    echo "<table border ='1' style='border-collapse: collapse'>";  
    for ($row=1; $row <= 10; $row++) {   
        echo "<tr> \n";  
        for ($col=1; $col <= 10; $col++) {   
            $p = $col * $row;  
            echo "<td>$p</td> \n";  
        }  
        echo "</tr>";  
    }  
    echo "</table>";
    

    i hope it will be helpful.