Jump to content

mysql/php array to javascript array


unknown1

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.