Jump to content

Help with javascript function return data on page


AJM2
Go to solution Solved by AJM2,

Recommended Posts

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>&#32;</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.

 

javascript_error.png

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.