Please provide help on how to assign php array values to javascript array
请提供有关如何将php数组值分配给javascript数组的帮助
----------- Using below php code I am storing data in php array -----------
-----------使用下面的php代码我在php数组中存储数据-----------
<?php
$str_query="SELECT title,description FROM tablename ORDER BY title";
$rs_sch=GetRecordset($str_query);
$int_count=0;
$schd_arr = array();
while(!$rs_sch->EOF())
{
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");
$rs_sch->MoveNext();
$int_count++;
}
?>
----- Using below javascript code I am trying to store php array data into javascript array -----
-----使用下面的javascript代码我试图将php数组数据存储到javascript数组-----
Please let me know what and how to write variable at below 2 mentioned locations so my code can work.
请让我知道在以下2个位置写入变量的内容和方式,以便我的代码可以正常工作。
<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
{
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;
}
</script>
Thank you in advance, KRA
先谢谢你,KRA
6 个解决方案
#1
35
You can't loop like that, you need to loop the PHP array and push
into javascript array:
你不能这样循环,你需要循环PHP数组并推入javascript数组:
<script type="text/javascript" language="javascript">
var pausecontent = new Array();
<?php foreach($schd_arr as $key => $val){ ?>
pausecontent.push('<?php echo $val; ?>');
<?php } ?>
</script>
#2
36
You can directly use the json_encode
function. It is easy to use and produces valid javascript so is very stable:
您可以直接使用json_encode函数。它易于使用并生成有效的JavaScript,因此非常稳定:
<script type="text/javascript">
var pausecontent = <?php echo json_encode($php_array); ?>;
</script>
#3
2
I think you'd best get the array to your javascript first. That would be something like this:
我认为你最好先把数组放到你的javascript中。这将是这样的:
var theVariableYouWantTheArrayIn = <?php echo json_encode($theArrayYouWantInJavascript); ?>
After that it is a normal js array, so you can use properties like .length which will presumably make the loop much easier.
之后它是一个普通的js数组,所以你可以使用像.length这样的属性,这可能会使循环变得更容易。
#4
0
Above both answers in good.
以上两个答案都很好。
- Iterate through the array you got as a result of your DB query and add each value to a new PHP variable,
- Add a counter and increment it during each iteration to make sure that you’re not adding a comma after the last element,
- If the count of the elements in the array is more than 1, create the array as usual, otherwise create a new array with 1 element and assign your PHP array value to the index 0.
迭代通过数据库查询得到的数组,并将每个值添加到新的PHP变量中,
添加一个计数器并在每次迭代期间递增它,以确保在最后一个元素之后不添加逗号,
如果数组中元素的数量大于1,则照常创建数组,否则创建一个包含1个元素的新数组,并将PHP数组值分配给索引0。
#5
0
I had the same problem doing this but finally found the best solution
我这样做有同样的问题,但最终找到了最佳解决方案
<script>
var arr=[];
arr.push('<?php
include('conn.php');
$query = "SELECT *FROM c group by name";
$res = mysqli_query($conn,$query) or die(mysqli_error($conn));
while($data = mysqli_fetch_array($res))
{
echo $data["name"];
}
?>');
</script>
#6
-1
You can just do like this with both:
你可以这样做:
for (sch_cnt=0; sch_cnt<?php echo $int_count; ?>; sch_cnt++)
#1
35
You can't loop like that, you need to loop the PHP array and push
into javascript array:
你不能这样循环,你需要循环PHP数组并推入javascript数组:
<script type="text/javascript" language="javascript">
var pausecontent = new Array();
<?php foreach($schd_arr as $key => $val){ ?>
pausecontent.push('<?php echo $val; ?>');
<?php } ?>
</script>
#2
36
You can directly use the json_encode
function. It is easy to use and produces valid javascript so is very stable:
您可以直接使用json_encode函数。它易于使用并生成有效的JavaScript,因此非常稳定:
<script type="text/javascript">
var pausecontent = <?php echo json_encode($php_array); ?>;
</script>
#3
2
I think you'd best get the array to your javascript first. That would be something like this:
我认为你最好先把数组放到你的javascript中。这将是这样的:
var theVariableYouWantTheArrayIn = <?php echo json_encode($theArrayYouWantInJavascript); ?>
After that it is a normal js array, so you can use properties like .length which will presumably make the loop much easier.
之后它是一个普通的js数组,所以你可以使用像.length这样的属性,这可能会使循环变得更容易。
#4
0
Above both answers in good.
以上两个答案都很好。
- Iterate through the array you got as a result of your DB query and add each value to a new PHP variable,
- Add a counter and increment it during each iteration to make sure that you’re not adding a comma after the last element,
- If the count of the elements in the array is more than 1, create the array as usual, otherwise create a new array with 1 element and assign your PHP array value to the index 0.
迭代通过数据库查询得到的数组,并将每个值添加到新的PHP变量中,
添加一个计数器并在每次迭代期间递增它,以确保在最后一个元素之后不添加逗号,
如果数组中元素的数量大于1,则照常创建数组,否则创建一个包含1个元素的新数组,并将PHP数组值分配给索引0。
#5
0
I had the same problem doing this but finally found the best solution
我这样做有同样的问题,但最终找到了最佳解决方案
<script>
var arr=[];
arr.push('<?php
include('conn.php');
$query = "SELECT *FROM c group by name";
$res = mysqli_query($conn,$query) or die(mysqli_error($conn));
while($data = mysqli_fetch_array($res))
{
echo $data["name"];
}
?>');
</script>
#6
-1
You can just do like this with both:
你可以这样做:
for (sch_cnt=0; sch_cnt<?php echo $int_count; ?>; sch_cnt++)