beennn Posted October 26, 2012 Share Posted October 26, 2012 I'm having some trouble putting a php array into a javascript array, here's what I have: <?php $table = array(); while($d = mysql_fetch_array($sDP)) {$table[] = $d; } ?> <script Language="JavaScript"> var description = new Array; <?php for($i=0; $i < 7; $i++) { echo "description[$i]='".$table[$i]."';\n";} ?> for(i = 0; i < 7; i++){ document.write(description[i]) } </SCRIPT> However document.write gives out "Array", though I'm not sure where it's coming from. I know it's not coming from my database, so there must be something wrong with the way I'm assigning the array? Link to comment Share on other sites More sharing options...
niche Posted October 27, 2012 Share Posted October 27, 2012 Why didn't you use a foreach loop? Link to comment Share on other sites More sharing options...
thescientist Posted October 27, 2012 Share Posted October 27, 2012 I would try something like this. var description = <?php json_encode($table) ?> Link to comment Share on other sites More sharing options...
Ingolme Posted October 27, 2012 Share Posted October 27, 2012 json_encode() is the best option. The reason your first script isn't working is because $table is a two-dimensional array and therefore requires two nested loops to read values from it. Link to comment Share on other sites More sharing options...
beennn Posted October 27, 2012 Author Share Posted October 27, 2012 Aha thank you very much. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now