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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.