unknown1 Posted September 10, 2009 Share Posted September 10, 2009 I want to take an array from a mysql database and make it into javascript variables e.g. $sql_nw = "select * from sit_details_tmp where id_site =" .$_GET['s_id']; $rec_nw = mysql_query($sql_nw) or die(mysql_error()); $num_nw = mysql_num_rows($rec_nw); // --------------- Display list of users -------------------------------// $rec_nw = mysql_query($sql_nw) or die(mysql_error()); while($rs_nw = mysql_fetch_array($rec_nw)) { echo "$rs_nw[url]"; } then take results and create a javascript array var $sitename = $rs_nw; Hope this explains what I'm trying to do... Quote Link to comment Share on other sites More sharing options...
unknown1 Posted September 10, 2009 Author Share Posted September 10, 2009 is that even possible?? if so can someone please explain how. Thanks!! Quote Link to comment Share on other sites More sharing options...
cbolson Posted September 10, 2009 Share Posted September 10, 2009 Hi, You could use the php json_encode() function. $sql_nw = "select * from sit_details_tmp where id_site =" .$_GET['s_id']; $rec_nw = mysql_query($sql_nw) or die(mysql_error()); $num_nw = mysql_num_rows($rec_nw); // --------------- Display list of users -------------------------------// $rec_nw = mysql_query($sql_nw) or die(mysql_error()); $url_array=array(); while($rs_nw = mysql_fetch_array($rec_nw)) { $url_array[]=$rs_nw["Url"]; } This creates a normal php array, now add it to the javascript and convert it using json_encode(): <script type="text/javascript"> ... your javsacript code var sitename=new Array(<?php echo json_encode($url_array); ?>); </script> Chris Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.