Page 1 of 1

Assign javascript vars using php and foreach...possible?

Posted: Sat Jan 20, 2007 11:15 pm
by bwv2
I have a database full of all kinds of goods that I need to print out in a javascript tree structure. I know javascript could access the database and assign the vars to js, but I'm already calling it using php for something else, so I want to kill 2 cats with one shovel.

So I want to call my database with php and write the values to an array. Then I want to loop through the array in php and write all variables to javascript variables. I could then use the variables later in the <script> section.

Here's an (incorrect) example of what I think should work. Use it as a jumping off point if you like...

Code: Select all

<?php
//----assume I already pulled all the array values out and stored them in array called $theArray

$i=1;
foreach($theArray as $value){
   echo '<script> var theVar'.$i.' = "'.= $fred2 .'"; </script>';
   $i++;
}

//----now test that at least the first js var was written...
?>
<script>
document.write(theVar1)
</script>
Is this possible? I can't get it to actually write the variables to javascript. I've messed with alterations of the above code, as my attempts usually result in fatal errors. Story of my life...

Posted: Sun Jan 21, 2007 2:14 am
by jmut
Yes it is possible. Check on what array could be in php.
And what array should look like in javascript. I mean syntax correct.

Then just build array2js() function that will transform the values of php into correct syntax of javascript.
Forget about script tags etc. Mind just values.

Re: Assign javascript vars using php and foreach...possible?

Posted: Sun Jan 21, 2007 8:22 am
by Mohamed
bwv2 wrote:I have a database full of all kinds of goods that I need to print out in a javascript tree structure. I know javascript could access the database and assign the vars to js, but I'm already calling it using php for something else, so I want to kill 2 cats with one shovel.

So I want to call my database with php and write the values to an array. Then I want to loop through the array in php and write all variables to javascript variables. I could then use the variables later in the <script> section.

Here's an (incorrect) example of what I think should work. Use it as a jumping off point if you like...

Code: Select all

<?php
//----assume I already pulled all the array values out and stored them in array called $theArray

$i=1;
foreach($theArray as $value){
   echo '<script> var theVar'.$i.' = "'.= $fred2 .'"; </script>';
   $i++;
}

//----now test that at least the first js var was written...
?>
<script>
document.write(theVar1)
</script>
Is this possible? I can't get it to actually write the variables to javascript. I've messed with alterations of the above code, as my attempts usually result in fatal errors. Story of my life...
yes it is possible and this is how I done it, I don't what is wrong your method.

Code: Select all

<?php
$php_array = array();
for($i=0; $i<10; $i++){
	$php_array[$i] = "DATa".$i;
}	
$i=0;
echo"<script>\nvar new_array = new Array();\n";
foreach ($php_array as $value){
	echo "new_array[".$i."]='$value';\n";
	$i++;
}
	echo "</script>\n";
?>
<script>
var str='';
for(i=0; i<10; i++){
	str+=new_array[i]+"<br>";
}
document.writeln(str);
</script>

Posted: Sun Jan 21, 2007 9:18 am
by Kieran Huggins
also - check out the JSON functions - they're specifically designed to create javascript arrays and objects.