DJ Howarth
DJ Howarth

Reputation: 582

Javascript/PHP Array

I edited my code, see below:

I am trying to do an array with the time enroute. $distance works properly.

Now "ete" is giving this result for each leg: NaN Hrs. NaN Mins.

I replaced distance with 1500 and it spit out a result. That leads me to believe this [new Array();] is the problem.

I did some more digging var distance = new Array(""); is giving me this result: 431,910,746,923 Does that mean the implode did not work?

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

We're getting closer I think...

 $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.";
    }
     }

Here is where Aircraft() is used:

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

Upvotes: 0

Views: 193

Answers (3)

MrFusion
MrFusion

Reputation: 911

If I understand correctly, you want to calculate several time estimates for different distances according to the selected aircraft.

To accomplish this, a JavaScript array with the distances could be stored. Then, the Aircraft() function would only need to iterate through the different spans filling in the new ETA's.

Try replacing the first block of code with this:

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

Upvotes: 0

DJ Howarth
DJ Howarth

Reputation: 582

Solved it:

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

Upvotes: 0

Cal
Cal

Reputation: 7157

Not an answer to your direct question, but you don't need to write code like this in PHP:

for($i = 0, $size = sizeof($Row1); $i < $size; ++$i){
if ($i < ($size-1)){

    $Row1[$i]['a']...
    $Row2[$i]['b']...
}
}

You can use foreach() to iterate over an array:

foreach ($Row1 as $k => $r1){
    $r2 = $Row2[$k];

    $r1['a']...
    $r2['a']...
}

That aside, your code does not do what you want for a couple of reasons. Assuming that the loop actually runs more than once, you're creating multiple functions all called Aircraft() - function names must be unique. Assuming you are actually running the function, then it will only run the most recently defined version of the function (i.e. the last one). You probably want something like this:

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

Upvotes: 2

Related Questions