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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

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

Post 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...
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
Mohamed
Forum Newbie
Posts: 21
Joined: Fri Jan 19, 2007 6:59 pm
Location: Seattle

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

Post 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>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

also - check out the JSON functions - they're specifically designed to create javascript arrays and objects.
Post Reply