addition of matrix in php -
i trying matrix addition in php. here trying input user , make array addition. tried this. have more mistakes here. 1 can give solution this... in advance...
<html> <body> <form name="form1" action="matrixaddjs.php" method="post"> enter number of rows matrix : <input type="text" name="ar"> enter number of columns 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 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 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 :"; 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>
if user gives:
enter number of rows matrix : 2
enter number of columns matrix : 2
enter matrix :
2 4
3 5
enter b matrix :
3 4
5 6
the resultant matrix is:
5 8
8 11
this expected result.
i don't know php code logic. code create matrix u wish , , having basic validation. use in real app, please according feasibility. just copy , paste code blank file , run on localhost.
<body> <form name="form1" action="matrixaddjs.php" method="post" onsubmit="return checkmatrix()"> enter number of rows matrix : <input type="text" name="ar" id="ar"> enter number of columns 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 matrix :'; htmla += '<table>'; (i = 0; < row; i++) { amatrix[i] = new array(); htmla += '<tr>'; (j = 0; j < col; j++) { htmla += '<td>'; htmla += '<input type="text" name="amatrix[' + + '][' + j + ']">'; htmla += '</td>'; } htmla += '</tr>'; } htmla += '</table>'; document.getelementbyid('matrixa').innerhtml = htmla; var bmatrix = new array(); var htmlb = 'enter b matrix :'; htmlb += '<table>'; (i = 0; < row; i++) { bmatrix[i] = new array(); htmlb += '<tr>'; (j = 0; j < col; j++) { htmlb += '<td>'; htmlb += '<input type="text" name="bmatrix[' + + '][' + 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 clicking on create matrix!"; exit; } echo "<table>"; echo "the resultant matrix :"; ($m = 0; $m < $ar; $m++) { echo "<tr>"; ($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>
Comments
Post a Comment