我编辑了我的代码,如下所示:
我正在尝试用途中时间做一个数组。 $distance 工作正常。
现在“ete”给出每条腿的结果:NaN Hrs。 NaN 分钟。
我将距离替换为 1500,它输出了结果。这让我相信这个 [new Array();] 就是问题所在。
我做了更多挖掘 var 距离 = new Array("");给我这个结果:431,910,746,923 这是否意味着内爆不起作用?
<script type="text/javascript">
var distance = new Array(<?=implode(', ', $distance)?>);
function Aircraft() {
var mylist = document.getElementById("myList");
for(var i = 0; i < distance.length; i++) {
var hour = (Math.floor(1500 / mylist.options[mylist.selectedIndex].value));
var minute = Math.round((Math.round(1500 / mylist.options[mylist.selectedIndex].value) - hour) * 60);
document.getElementById("ete" + i).innerHTML = hour + " Hrs. " + minute + " Mins.";
}
}
</script>
我认为我们越来越接近了......
$distance = array();
for($i = 0, $size = sizeof($Row1); $i < ($size - 1); ++$i){
$distance[$i] = ROUND((ACOS(SIN($Row2[$i][4] * PI() / 180) * SIN($Row1[$i][4] * PI() / 180) + COS($Row2[$i][4] * PI() / 180) * COS($Row1[$i][4] * PI() / 180) * COS(($Row2[$i][5] - $Row1[$i][5]) * PI() / 180)) * 180 / PI()) * 60 );
echo "<td width=100>" . $distance[$i] . " NM</td>";
echo "<td width=100><span id=\"ete" . $i . "\"></span></td>";
}
?>
<script type="text/javascript">
var distance = new Array(<?=implode(', ', $distance)?>);
function Aircraft() {
var mylist = document.getElementById("myList");
for(var i = 0; i < distance.length; i++) {
var hour = (Math.floor(distance / mylist.options[mylist.selectedIndex].value));
var minute = Math.round((Math.round(distance / mylist.options[mylist.selectedIndex].value) - hour) * 60);
document.getElementById("ete" + i).innerHTML = hour + " Hrs. " + minute + " Mins.";
}
}
这是使用 Aircraft() 的地方:
<select id=\"myList\" style=\"width:150px;\" onchange=\"Aircraft()\">
<option>Select Aircraft</option>
<option value=\"300\">King Air 350</option>
<option value=\"450\">G-V</option>
<option value=\"470\">GLEX</option>
<option value=\"350\">Astra</option>
</select>
最佳答案
不是您直接问题的答案,但您不需要在 PHP 中编写这样的代码:
for($i = 0, $size = sizeof($Row1); $i < $size; ++$i){
if ($i < ($size-1)){
$Row1[$i]['a']...
$Row2[$i]['b']...
}
}
您可以使用foreach()
来迭代数组:
foreach ($Row1 as $k => $r1){
$r2 = $Row2[$k];
$r1['a']...
$r2['a']...
}
除此之外,由于几个原因,您的代码无法执行您想要的操作。假设循环实际上运行多次,您将创建多个均名为 Aircraft()
的函数 - 函数名称必须是唯一的。假设您实际上正在运行该函数,那么它将仅运行该函数的最近定义的版本(即最后一个)。您可能想要这样的东西:
<table>
<?
# output table cells we will populate
foreach (....){
echo "<tr>";
echo "<td id=\"$id\"></td>";
echo "</tr>";
}
?>
</table>
<script>
function Aircraft(distance, id){
body of javascript here,
inserts results into element with ID of `id`
}
<?
# now output calls to the JS function that will produce the output
foreach (....){
$distance = ...;
echo "Aircraft($distance, $id);\n";
}
?>
</script>
关于php - JavaScript/PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068279/