Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,944
40
Australia
Hey all, this code that I've written below is not working properly, I'll explain more below.
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
}
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?
 
Instead of,
PHP:
	var theVar = "tb1";
	var tempArray = window[theVar]; //getting the variable
try,
PHP:
	var tempArray = window[tb1]; //getting the variable
Also, I believe you mean tempArray[0].length should be 3, not just tempArray.length. The fact that tempArray.length results in 1 matches that you see tempArray[1][x] doesn't exist, because that's looking at the first array index.
 
Instead of,
Unfortunately, the table names I provided are only simple examples, there will be many more so I need to do it dynamically.

Also, I've even tried doing tb1.length (using the hard coded name) and it still returns 1, when it should be 3.
Also, I believe you mean tempArray[0].length should be 3, not just tempArray.length. The fact that tempArray.length results in 1 matches that you see tempArray[1][x] doesn't exist, because that's looking at the first array index.
The first array index should be returning 3, as is being echo'd out by my PHP script, unfortunately it only shows a length of 1.

Any thoughts?
 
Unfortunately, the table names I provided are only simple examples, there will be many more so I need to do it dynamically.

Then,
PHP:
var tempArray = window['tb1'];
I was simply trying to get across that you could do it in one line rather than two.

It's hard to tell much else. Can you show more of the actual resulting JavaScript produced? Maybe an entire for loop's worth. It seems like there's a potential that some variables could become overwritten, but it's hard to tell from the code.
 
Thanks for your reply,
theVar stores the text from a control on my form, it's actually quite long, so I'd rather keep it separated.

This is the javascript output when the information output via echo in the if ($result) statement and in the while loop.

Code:
var tb1 = new Array(1);
tb1[0] = new Array(3);

tb1[0][0] = 'test1';
tb1[0][1] = 'test2';
tb1[0][2] = 'test3';
var tb2 = new Array(1);
tb2[0] = new Array(3);

tb2[0][0] = 'test1';
tb2[0][1] = 'test2';
tb2[0][2] = 'test3';
tb2[1][0] = 'test1';
tb2[1][1] = 'test2';
tb2[1][2] = 'test3';
tb2[2][0] = 'test1';
tb2[2][1] = 'test2';
tb2[2][2] = 'test3';
var tb3 = new Array(1);
tb3[0] = new Array(3);
tb3[0][0] = 'test1';
tb3[0][1] = 'test2';
tb3[0][2] = 'test3';
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.