在php中添加矩阵

时间:2013-08-07 10:18:30

标签: php matrix add

我正在尝试在php中添加矩阵。在这里,我试图从用户获取输入并使数组添加。我试过这个。我在这里有更多的错误。任何人都可以为此提供解决方案......在此先感谢...

<html>
<body>
<form name="form1" action="matrixaddjs.php" method="post">
Enter the number of rows for matrix : <input type="text" name="ar">
Enter the number of columns for matrix : <input type="text" name="ac">
<input type="submit" name="submit" value="submit">
<script>
var row=document.getElementById('ar').value;
var col=document.getElementById('ac').value;
var i;
var j;
var k;
var l;
var amatrix= new array();
document.write('<table>');
for(i=0;i<row;i++)
{
    amatrix[i]=new array(j);
            document.write('Enter the A matrix :'); 
    document.write('<tr>');
    for(j=0;j<col;j++)
    {
        document.write('<td>');
        document.write('<input type="text" name="amatrix[i][j]">');
        document.write('</td>');
    }
document.write('</tr>');
}
document.write('</table>');


var bmatrix= new array();
    document.write('Enter the B matrix :');
document.write('<table>');
for(i=0;i<row;i++)
{
    bmatrix[i]=new array(j);
    document.write('<tr>');
    for(j=0;j<col;j++)
    {
        document.write('<td>');
        document.write('<input type="text" name="bmatrix[i][j]">');
        document.write('</td>');
    }
document.write('</tr>');
}
document.write('</table>');
</script>
</form>
<?php
if($_POST['submit']=== 'submit')
{
$amatrix=$_POST['amatrix'];
$bmatrix=$_POST['bmatrix'];
echo "<table>";
    echo "The resultant matrix is :";
for($m=0;$m<$ar;$m++)
{
    echo "<tr>";
    for($n=0;$n<$ac;$n++)
    {
        $cmatrix[$m][$n]=$amatrix[$m][$n]+$bmatrix[$m][$n];
        echo "<td>";
        echo $cmatrix[$m][$n];
        echo "</td>";
    }
    echo "</tr>";

}
echo "</table>";
}
?>

</body>
</html>

如果用户提供:

输入矩阵的行数:2

输入矩阵的列数:2

输入A矩阵:

2 4

3 5

输入B矩阵:

3 4

5 6

结果矩阵是:

5 8

8 11

这是我的预期结果。

2 个答案:

答案 0 :(得分:2)

我不知道你的PHP代码逻辑。但是这段代码会根据你的意愿创建一个矩阵,并进行一些基本的验证。要在真实应用中使用,请根据您的可行性。 只需将此代码复制并粘贴到空白文件中,然后在localhost上运行。     

<body>
    <form name="form1" action="matrixaddjs.php" method="post" onsubmit="return checkMatrix()">
        Enter the number of rows for matrix :
        <input type="text" name="ar" id="ar">
        Enter the number of columns for matrix :
        <input type="text" name="ac" id="ac">
        <input type="button" name="create" value="Create Matrix" onclick="createMatrix()">
        <div id="matrixA">
        </div>
        <div id="matrixB">
        </div>
        <input type="submit" name="submit" value="submit">
        <script>
            function createMatrix() {
                var row = document.getElementById('ar').value;
                var col = document.getElementById('ac').value;
                var i;
                var j;
                var k;
                var l;
                if (row == '') {
                    alert('Please Enter Number Of Rows!');
                    return false;
                }
                if (col == '') {
                    alert('Please Enter Number Of Columns !');
                    return false;
                }

                var amatrix = new Array();

                var htmlA = 'Enter the A matrix :';
                htmlA += '<table>';

                for (i = 0; i < row; i++) {
                    amatrix[i] = new Array();

                    htmlA += '<tr>';
                    for (j = 0; j < col; j++) {
                        htmlA += '<td>';
                        htmlA += '<input type="text" name="amatrix[' + i + '][' + j + ']">';
                        htmlA += '</td>';
                    }
                    htmlA += '</tr>';
                }
                htmlA += '</table>';
                document.getElementById('matrixA').innerHTML = htmlA;

                var bmatrix = new Array();
                var htmlB = 'Enter the B matrix :';
                htmlB += '<table>';
                for (i = 0; i < row; i++) {
                    bmatrix[i] = new Array();
                    htmlB += '<tr>';
                    for (j = 0; j < col; j++) {
                        htmlB += '<td>';
                        htmlB += '<input type="text" name="bmatrix[' + i + '][' + j + ']">';
                        htmlB += '</td>';
                    }
                    htmlB += '</tr>';
                }
                htmlB += '</table>';
                document.getElementById('matrixB').innerHTML = htmlB;
            }

            function checkMatrix() {
                var row = document.getElementById('ar').value;
                var col = document.getElementById('ac').value;

                if (row == '' || col == '') {
                    alert('Please Create Matrix First!');
                    return false;
                }
                else {
                    return true;
                }
            }
        </script>
    </form>
     <?php
if ($_POST['submit'] === 'submit') {
$amatrix = $_POST['amatrix'];
$bmatrix = $_POST['bmatrix'];
if (!is_array($amatrix) && !is_array($bmatrix)) {
    echo "Please Create Matrix First by Clicking on Create Matrix!";
    exit;
}
echo "<table>";
echo "The resultant matrix is :";
for ($m = 0; $m < $ar; $m++) {
    echo "<tr>";
    for ($n = 0; $n < $ac; $n++) {
        $cmatrix[$m][$n] = $amatrix[$m][$n] + $bmatrix[$m][$n];
        echo "<td>";
        echo $cmatrix[$m][$n];
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";
}
?>
</body>

答案 1 :(得分:1)

#1

如果您使用document.getElementById,则必须有ID而不是NAME

<input type="text" name="ar" id="ar">

#2

将生成的A和B矩阵代码放入函数中。把它们放在DIV中

<a href="javascript:renderMatrix();">Render</a>
<div id="target"></div>
<script>
  function renderMatrix() {
    var html = '';
    html += '<table>';  // use a variable instead of document.write
    ...
    html += '<input type="text" name="amatrix['+i+']['+j+']">';  // !!!
    ...
    document.getElementById('target').innerHTML = html;
  }
</script>