Hey guys I have a weird problem trying to use a global array in php.
I don't even really want to use a global but here's what i'm trying to do.
I have 2 functions
createMatrix($name) which creates a 5x5 matrix of texts boxes and remembers values loadMatrix($name) which loads a 5x5 matrix from post data.
For some reason, after I run loadMatrix(), the values I set are lost in createMatrix()
I've tried a ton of debugging methods and declaring my matrix variables as globals but no luck. I know it's a scope issue because when I tried running everything outside of the functions it worked just fine.
<form action="Program4.php" method="post">
<?php
$matrixa = array(array());
$matrixb = array(array());
if($_POST['add'])
echo "Add Clicked";
if($_POST['subtract'])
echo "Subtract Clicked";
function loadArrays($name1,$name2) {
//The Solution: global $matrixa, $matrixb;
for($x = 0; $x < 5; $x++)
for($y = 0; $y < 5; $y++) {
$matrixa[$x][$y] = $_POST[$name1 . $x . $y];
$matrixb[$x][$y] = $_POST[$name2 . $x . $y];
}
}
function createMatrix($name) {
//The Solution: global $matrixa, $matrixb;
echo "<h2>" . $name . "</h2>\n";
for($x = 0; $x < 5; $x++)
echo $matrixa[0][$x];
echo "<div style=\"border:ridge; border-width:7px; padding:12px; float:left; background-color:gray\">\n";
for($x = 0; $x < 5; $x++) {
for($y = 0; $y < 5; $y++) {
echo "<input type=\"text\" name=\"" . $name . $x . $y . "\" maxlength=\"6\" style=\"width:50px\" value=\"";
if($name == "MatrixA")
echo "" . $matrixa[$x][$y];
else if($name == "MatrixB")
echo "" . $matrixb[$x][$y];
echo "\">\n";
}
echo "<br>\n";
}
echo "</div>\n";
echo "<br><br><br><br><br><br><br><br><br>\n";
}
loadArrays("MatrixA","MatrixB");
createMatrix("MatrixA");
createMatrix("MatrixB");
?>
<input type="submit" value="Add Matrices" name="add" />
<input type="submit" value="Subtract Matrices" name="subtract" />
</form>
This project is due at midnight so I'd appreciate some help :)
Basically the problem is trying to save the values. I am able to load the variables from post data into the arrays using loadArrays() just fine, but the values don't show up AT ALL when I use createMatrix();
loadMatrix()anywhere on your code.global $matrixa;