AJM2 Posted January 26, 2022 Share Posted January 26, 2022 I am having trouble getting this javascript to function. It is supposed to take the compass heading and return a character based on the heading. N, S, E, W... <script> function degToCompass(num) { var val = Math.floor((num / 22.5) + 0.5); var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]; return arr[(val % 16)]; } </script> I have the function in the header.php page and include it at the top of this page <?php include_once("dbconnect.php"); include_once("header.php"); //error dump for testing error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //echo "Connected successfully"; ?> <?php //$today = strtotime('today midnight'); //$yesterday = date('d.m.Y', strtotime("-1 days")); $sql = " SELECT avg(winddir) as avgwinddir , MAX(temp) as maxtemp , MIN(temp) as mintemp , AVG(temp) as avgtemp , MAX(dewpoint) as maxdewpoint , MIN(dewpoint) as mindewpoint , AVG(dewpoint) as avgdewpoint , MAX(humidity) as maxhumidity , MIN(humidity) as minhumidity , AVG(humidity) as avghumidity , MAX(dailyrain) as maxdailyrain , MIN(dailyrain) as mindailyrain , AVG(dailyrain) as avgdailyrain , MAX(windspeed) as maxwindspeed , MIN(windspeed) as minwindspeed , AVG(windspeed) as avgwindspeed , MAX(gust) as maxgust , MIN(gust) as mingust , AVG(gust) as avggust , MAX(stationbarometer) as maxstationbarometer , MIN(stationbarometer) as minstationbarometer , AVG(stationbarometer) as avgstationbarometer FROM weather_data WHERE DateTime between '2022-01-26 00:00:00.000000' and '2022-01-27 00:00:00.000000' LIMIT 1; "; $result = $conn->query($sql); if ($result->num_rows > 0) { echo " <table border=1 width=100%> <tr> <td colspan=2 align=center><b>Summary Weather Data</b></td> </tr> <tr> <td colspan=2 align=center></td> </tr> <tr> <td align=center> <table border=1 width=90%> <th> </th><th>High</th><th>Low</th><th>Average</th> </tr>"; //output data of each row while($row = $result->fetch_assoc()) { echo "<tr> <td><b>Temperature</b></td><td>".$row["maxtemp"]. "</td><td>".$row["mintemp"]. "</td><td>".$row["avgtemp"]. "</td> </tr> <tr> <td><b>Dew Point</b></td><td>".$row["maxdewpoint"]. "</td><td>".$row["mindewpoint"]. "</td><td>".$row["avgdewpoint"]. "</td> </tr> <tr> <td><b>Humidity</b></td><td>".$row["maxhumidity"]. "</td><td>".$row["minhumidity"]. "</td><td>".$row["avghumidity"]. "</td> </tr> <tr> <td><b>Precipitation</b></td><td>".$row["maxdailyrain"]. "</td><td>".$row["maxdailyrain"]. "</td><td>".$row["avgdailyrain"]. "</td> </tr> </table> </td> <td align=center> <table border=1 width=90%> <tr> <th> </th><th>High</th><th>Low</th><th>Average</th> </tr> <tr> <td><b>Wind Speed</b></td><td>".$row["maxwindspeed"]. "</td><td>".$row["minwindspeed"]. "</td><td>".$row["avgwindspeed"]. "</td> </tr> <tr> <td><b>Wind Gust</b></td><td>".$row["maxgust"]. "</td><td>==</td><td>".$row["avggust"]. "</td> </tr> <tr> <td><b>Wind Direction</b></td><td>--</td><td>' <script>degToCompass(.$row["avgwinddir"].);</script>';</td><td>".$row["avgwinddir"]. "</td> </tr> <tr> <td><b>Barometer</b></td><td>".$row["maxstationbarometer"]. "</td><td>".$row["minstationbarometer"]. "</td><td>".$row["avgstationbarometer"]. "</td> </tr> </table> </td> </tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> I can't figure out why the function won't display. I even broke it down to a basic page with only the function and trying to output on the page and nothing returns but "This is a test." <!DOCTYPE html> <html> <head> <script> function degToCompass(num) { var val = Math.floor((num / 22.5) + 0.5); var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]; return arr[(val % 16)]; } </script> </head> <body> <?php echo '<script type="text/javascript"> degToCompass(180); </script>'; echo "This is a test." ?> </body> </html> This is the field on the page I am trying to have the function display Any pointers would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/314466-help-with-javascript-function-return-data-on-page/ Share on other sites More sharing options...
Barand Posted January 26, 2022 Share Posted January 26, 2022 Why don't you use a php function? 1 Quote Link to comment https://forums.phpfreaks.com/topic/314466-help-with-javascript-function-return-data-on-page/#findComment-1593647 Share on other sites More sharing options...
Solution AJM2 Posted January 27, 2022 Author Solution Share Posted January 27, 2022 I found this searching the internet searching for a way to convert compass heading to letters. I will look into php functions. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314466-help-with-javascript-function-return-data-on-page/#findComment-1593658 Share on other sites More sharing options...
AJM2 Posted January 27, 2022 Author Share Posted January 27, 2022 Barand, Thanks for the tip. I did some research and converted the javascript function to a php function as you suggested. It's working now! function degOfCompass($num) { $val = floor(($num / 22.5) + 0.5); $arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]; return $arr[($val % 16)]; //return $num; } Quote Link to comment https://forums.phpfreaks.com/topic/314466-help-with-javascript-function-return-data-on-page/#findComment-1593660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.