Hey all, this code that I've written below is not working properly, I'll explain more below.
$tablesList is an array containing all tables in one of my MySQL databases. As of now, there are three tables in this database, let's call them "tb1", "tb2", and "tb3". Each table contains three columns, let's call them "test1", "test2", "test3".
Just for your information, $result is always true, and all the javascript syntax is correct, unless I've missed something, which, I'm sure I haven't.
Also, tb1 contains one row, tb2 contains three rows, tb3 contains one row, fyi.
I'm creating a two dimensional javascript array containing the data inside of these tables, so an example of the output of the while loop when $i is 0 could be:
Now the code itself runs fine, I've echo'd it out as HTML instead of javascript and the output looks exactly as intended, but this code doesn't seem to agree with that:
The problem here is that tempArray.length returns 1, when it should in fact return 3.
tempArray[0][0], [0][1] and [0][2] all reflect the values in tb1[0][0], [0][1] and [0][2], but tempArray[1][0] and any other value greater than 0 in that first index do not exist, but the code is being written.
It's the same for tempArray[1][x] and tempArray[2][x], although sometimes even tempArray[2][x] shows undefined, I don't know why, I think my last change to the above code which was moving the "<script type=..." and "</script>" echo's inside the for loop, compared to outside of it, fixed that issue -- and I have NO idea why.
I'll also add, that using tb1.length (the exact name) rather than getting it through window[] produces the same results.
Any idea's what's going on here?
PHP:
for ($i = 0; $i<count($tablesList); $i++) {
echo "<script type='text/javascript'>";
$sql = "SELECT * FROM ".$tablesList[$i];
$result = mysql_query($sql, $con);
if ($result) {
echo "var ".$tablesList[$i]." = new Array(".count($tablesList[$i]).");";
for ($k = 0; $k < count($tablesList[$i]); $k++) {
echo $tablesList[$i]."[".$k."] = new Array(3);";
}
}
$z = 0;
while ($row = mysql_fetch_array($result)) {
echo $tablesList[$i]."[".$z."][0] = '".$row["test1"]."';";
echo $tablesList[$i]."[".$z."][1] = '".$row["test2"]."';";
echo $tablesList[$i]."[".$z."][2] = '".$row["test3"]."';";
$z += 1;
}
echo "</script>";
}
$tablesList is an array containing all tables in one of my MySQL databases. As of now, there are three tables in this database, let's call them "tb1", "tb2", and "tb3". Each table contains three columns, let's call them "test1", "test2", "test3".
Just for your information, $result is always true, and all the javascript syntax is correct, unless I've missed something, which, I'm sure I haven't.
Also, tb1 contains one row, tb2 contains three rows, tb3 contains one row, fyi.
I'm creating a two dimensional javascript array containing the data inside of these tables, so an example of the output of the while loop when $i is 0 could be:
tb1[0][0] = 'value1'; //this is the first row in the column test1 in the table tb1
tb1[0][1] = 'value2'; //this is the first row in the column test2 in the table tb1
tb1[0][2] = 'value3';
tb1[1][0] = 'value1'; //this is the second row in the column test1 in the table tb1
tb1[1][1] = 'value2'; //this is the second row in the column test2 in the table tb1
tb1[1][2] = 'value3';
Now the code itself runs fine, I've echo'd it out as HTML instead of javascript and the output looks exactly as intended, but this code doesn't seem to agree with that:
Code:
function loadInfo() {
var theVar = "tb1";
var tempArray = window[theVar]; //getting the variable
}
tempArray[0][0], [0][1] and [0][2] all reflect the values in tb1[0][0], [0][1] and [0][2], but tempArray[1][0] and any other value greater than 0 in that first index do not exist, but the code is being written.
It's the same for tempArray[1][x] and tempArray[2][x], although sometimes even tempArray[2][x] shows undefined, I don't know why, I think my last change to the above code which was moving the "<script type=..." and "</script>" echo's inside the for loop, compared to outside of it, fixed that issue -- and I have NO idea why.
I'll also add, that using tb1.length (the exact name) rather than getting it through window[] produces the same results.
Any idea's what's going on here?