0

I have a php code but don't know how to show it in HTML table. I created matrix tables to add and multiply but don't know how to put results in HTML table. I tried to put the code between script tags but it didn't work. My matrix func basically generates 4 by 4 matrices with random integers and generates two matrices called A and B. then I created two other functions to add and multiply these matrices but I didn't print them out in HTML table. My code:

// creating function addMatrix which takes two numbers and returns a new matrix
function addMatrix($A, $B)
{
    // declaring $ADD as a empty array
    $ADD = array();
    // iterate from i is equal to 0, i is less than 4 and increment i by 1
    for ($i = 0; $i < 4; $i++) {
        // iterate from j is equal to 0, j is less than 4 and increment j by 1
        for ($j = 0; $j < 4; $j++) {
            // adding $A[$i][$j] to $B[$i][$j] and store the result in $ADD[$i][$j]
            $ADD[$i][$j] = $A[$i][$j] + $B[$i][$j];
        }
    }
    // return $ADD
    return $ADD;
}

// creating function mulMatrix which takes two numbers and returns a new matrix
function mulMatrix($A, $B)
{
    // declaring $MUL as a empty array
    $MUL = array();
    // iterate from i is equal to 0, i is less than 4 and increment i by 1
    for ($i = 0; $i < 4; $i++) {
        // iterate from j is equal to 0, j is less than 4 and increment j by 1
        for ($j = 0; $j < 4; $j++) {
            // assign 0 in $MUL[$i][]
            $MUL[$i][] = 0;
            // iterate from k is equal to 0, k is less than 4 and increment k by 1
            for ($k = 0; $k < 4; $k++) {
                // multiply $ADD[$i][$k] with $B[$k][$j] then add it in $MUL[$i][$j] and store it
                // in $MUL[$i][$j]
                $MUL[$i][$j] += $A[$i][$k] * $B[$k][$j];
            }
        }
    }
    // return $MUL
    return $MUL;
}


// generate matrix by calling generateMatrix and store it in $A
$A = generateMatrix();
// generate matrix by calling generateMatrix and store it in $B
$B = generateMatrix();
// add matrix by calling addMatrix and store it in $ADD
$ADD = addMatrix($A, $B);
// mul matrix by calling mulMatrix and store it in $MUL
$MUL = mulMatrix($A, $B);

// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
    // create element tr
    echo "<tr>";
    // iterate from j is equal to 0, j is less than 4 and increment j by 1
    for ($j = 0; $j < 4; $j++) {
        // display value of $A[$i][$j] with element td
        echo "<td>" . $A[$i][$j] . "</td>";
    }
    // create element end tr table
    echo "</tr>";
}
// create element end tag table
echo "</table>";
// create element br
echo "<br>";

// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'B'</th>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($i = 0; $i < 4; $i++) {
    // create element tr
    echo "<tr>";
    // iterate from j is equal to 0, j is less than 4 and increment j by 1
    for ($j = 0; $j < 4; $j++) {
        // display value of $A[$i][$j] with element td
        echo "<td>" . $B[$i][$j] . "</td>";
    }
    // create element end tr table
    echo "</tr>";
}
// create element end tr table
echo "</table>";


// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' + 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
    // create element tr
    echo "<tr>";
    // iterate from j is equal to 0, j is less than 4 and increment j by 1
    for ($j = 0; $j < 4; $j++) {
        // display value of $ADD[$i][$j] with element td
        echo "<td>" . $ADD[$i][$j] . "</td>";
    }
    // create element end tr table
    echo "</tr>";
}
// create element end tr table
echo "</table>";

// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' * 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
    // create element tr
    echo "<tr>";
    // iterate from j is equal to 0, j is less than 4 and increment j by 1
    for ($j = 0; $j < 4; $j++) {
        // display value of $MUL[$i][$j] with element td        
        echo "<td>" . $MUL[$i][$j] . "</td>";
    }
    // create element end tr table
    echo "</tr>";
}
// create element end tr table
echo "</table>";here
  • There is already a question answered about this subject: https://stackoverflow.com/questions/14782751/convert-pdf-to-html-in-php – TDwebdev Apr 09 '22 at 09:39
  • 1
    it is not the thing that I asked. There is nothing to do with pdf??? – franchise2121 Apr 09 '22 at 09:50
  • Can you clarify what happens when you run the code you've shown? Does the HTML table not display at all, display the wrong information, display some values but not others...? – IMSoP Apr 09 '22 at 10:11
  • it doesnt return anything when i copy paste code to html – franchise2121 Apr 09 '22 at 10:33
  • Fist of all, enable error reporting, that will help you solve this issue better then us. Function `generateMatrix` is not present in your question, could be wrong, who knows. The `Matrix 'A'` etc. should be wraped inside ``, but i dubt thats the cause, browsers autocorrect these. Otherwise everything looks fine except the `here` typo at the end of file. – Kazz Apr 09 '22 at 11:23
  • You haven't defined generateMatrix(). brain@earth:/tmp$ php x.php PHP Fatal error: Uncaught Error: Call to undefined function generateMatrix() in /tmp/x.php:45 As @Kazz said, set display_errors in php.ini to on. restart your webserver. Otherwise you just see blank page if there is error in your code. – Brain90 Apr 09 '22 at 12:40

1 Answers1

0

I can't see any HTML tag. Maybe it's a partial codebase because the definition of generateMatrix is also missing. You can try this syntax to print the matrix,

    <!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A'</th>
    <?php
        // iterate from i is equal to 0, i is less than 4 and increment i by 1
        for ($i = 0; $i < 4; $i++) {
            // create element tr
            echo "<tr>";
            // iterate from j is equal to 0, j is less than 4 and increment j by 1
            for ($j = 0; $j < 4; $j++) {
                // display value of $A[$i][$j] with element td
                echo "<td>" . $A[$i][$j] . "</td>";
            }
            // create element end tr table
            echo "</tr>";
        }
    ?>
<!--  create element end tag table -->
</table>
<!-- create element br -->
<br>

<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'B'</th>
    <?php 
        // iterate from j is equal to 0, j is less than 4 and increment j by 1
        for ($i = 0; $i < 4; $i++) {
            // create element tr
            echo "<tr>";
            // iterate from j is equal to 0, j is less than 4 and increment j by 1
            for ($j = 0; $j < 4; $j++) {
                // display value of $A[$i][$j] with element td
                echo "<td>" . $B[$i][$j] . "</td>";
            }
            // create element end tr table
            echo "</tr>";
        }
    ?>
<!-- create element end tr table -->
</table>
<!-- create element br -->
<br>

<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A' + 'B'</th>
    <?php
        // iterate from i is equal to 0, i is less than 4 and increment i by 1
        for ($i = 0; $i < 4; $i++) {
            // create element tr
            echo "<tr>";
            // iterate from j is equal to 0, j is less than 4 and increment j by 1
            for ($j = 0; $j < 4; $j++) {
                // display value of $ADD[$i][$j] with element td
                echo "<td>" . $ADD[$i][$j] . "</td>";
            }
            // create element end tr table
            echo "</tr>";
        }
    ?>
<!-- create element end tr table -->
</table>

<!-- create element br -->
<br>
<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A' * 'B'</th>
    <?php
        // iterate from i is equal to 0, i is less than 4 and increment i by 1
        for ($i = 0; $i < 4; $i++) {
            // create element tr
            echo "<tr>";
            // iterate from j is equal to 0, j is less than 4 and increment j by 1
            for ($j = 0; $j < 4; $j++) {
                // display value of $MUL[$i][$j] with element td        
                echo "<td>" . $MUL[$i][$j] . "</td>";
            }
            // create element end tr table
            echo "</tr>";
        }
    ?>

<!-- create element end tr table -->
</table>

Output

Ratul
  • 43
  • 2
  • 8