我编辑了我的代码,见下文:
我正在尝试使用途中的时间做一个数组。 $distance工作正常。
现在"ete"为每条腿给出这个结果:NaN Hrs. NaN Mins。
我用 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>
解决了:
<script type="text/javascript">
var distance = new Array("<?php echo implode('","',$distance)?>");
function Aircraft() {
var mylist = document.getElementById("myList");
for(var i = 0; i < distance.length; i++) {
var hour = (Math.floor(distance[i] / mylist.options[mylist.selectedIndex].value));
var minute = Math.round(((distance[i] / mylist.options[mylist.selectedIndex].value) - hour) * 60);
document.getElementById("ete" + i).innerHTML = hour + " Hrs. " + minute + " Mins.";
}
}
</script>
如果我
理解正确,您想根据所选飞机计算不同距离的几个时间估计。
为了实现这一点,可以存储一个带有距离的 JavaScript 数组。 然后,Aircraft()
函数只需要遍历填充新ETA的不同跨度。
尝试将第一个代码块替换为以下内容:
<?php
$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 distances = new Array("<?=implode('", "', $distance)?>");
function Aircraft() {
var mylist = document.getElementById("myList");
for(var i = 0; i < distances.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.";
}
}
</script>