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... Link to comment https://forums.phpfreaks.com/topic/173815-mysqlphp-array-to-javascript-array/ 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!! Link to comment https://forums.phpfreaks.com/topic/173815-mysqlphp-array-to-javascript-array/#findComment-916256 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 Link to comment https://forums.phpfreaks.com/topic/173815-mysqlphp-array-to-javascript-array/#findComment-916391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.