![](/img/trans.png)
[英]how can i generate Javascript and with PHP and put it into a .js file
[英]How can I put an array(that is in php) into the table(js)
所以我试图将 php 中的数组放入 js 中的表中。 php 连接到服务器,我将其放入阵列中。 数组是:
array(
[0] =>Array([file] => 292929)
[1] =>Array([file] => 1321323)
[2] =>Array([file] => 1232312)
)
所以我试图将上面的数字放入 js 中的表格中。
<script>
var counter = 0;
var max = //gets the total number in array
<?php
$numc = 0
?>
while(parseInt(max) > counter)
{
var table = document.getElementById("myTable");//Table
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
//Variables in cells
cell1.innerHTML = "Name";
cell2.innerHTML = '<?php echo "{$rows[$numc]['file']}"; ?>';
counter = counter + 1;
<?php
$numc = 1 + $numc;
?>
}
</script>
我期望的类似于这张表:
Name 292929
Name 1321323
Name 1232312
除了我得到
Name 292929
Name 292929
Name 292929
我知道这是 scope 吗? php 的问题,但我无法将所有 php 值放入表中。
首先你只用$rows[$numc]['file']
渲染代码一次 - js while cycle is not reachable in php
你可以:
<?php
echo 'const numArray = [' . implode(', ', $rows) . '];';
?>
并在 js 中使用它,因为它现在是 js 中的 js 数组:) (在 js 中制作 numc,在 js 中递增它等等)
<script>
var counter = 0;
<?php
echo 'const numArray = [' . implode(', ', $rows) . '];';
?>
var max = numArray.length;
var table = document.getElementById("myTable");//Table
while(max > counter)
{
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
//Variables in cells
cell1.innerHTML = "Name";
cell2.innerHTML = numArray[counter];
counter = counter + 1;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.