Jump to content

AJM2

Members
  • Posts

    15
  • Joined

  • Last visited

Everything posted by AJM2

  1. I guess I was over complicating this thought process. Thank you for the assistance. Very much appreciated.
  2. I am not sure I am doing this correctly. My goal is to have a button on each side of the page with today's date in the middle as dropdown lists allowing to page through dates (see image) or pick a specific date. I have the code somewhat of working. It will page up tomorrow, next day, next day... and it goes down, yesterday, day before... when starting at today. But once I go either up or down I can't page back in the opposite direction. The ultimate goal is to pass the "current" date into a query to get today's data as you page through. I hope I described this well enough. I don't want to use a date picker where a calendar pops up. Any pointers would be appreciated. <html> <head> <title></title> </head> <body> <form action="calendar_form.php" method="post" name="f1"> <?php error_reporting(E_ALL); ini_set('display_errors', '1'); // Default dates $beginYesterday = date('Y-m-d H:i:s',strtotime('yesterday')); $beginOfDay = date('Y-m-d H:i:s',strtotime('today')); $endOfDay = date('Y-m-d H:i:s',strtotime('tomorrow')); $tomorrow = date('Y-m-d H:i:s', strtotime($beginOfDay . '+1 days')); $f_today = date('m-d-Y', strtotime('today midnight')); $yesterday = date('Y-m-d H:i:s', strtotime('-1 days')); // Check the form was submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { $yesterday = $_POST['yesterday']; $tomorrow = $_POST['tomorrow']; if($_POST['Submit'] == 'Tomorrow'){ $tomorrow = date('Y-m-d H:i:s', strtotime($tomorrow . '+1 days')); $today = date('m-d-Y', strtotime($_POST['tomorrow'])); $yesterday = date('Y-m-d H:i:s', strtotime($tomorrow . '-1 days')); $f_today = $today; } elseif($_POST['Submit'] == 'Yesterday'){ $tomorrow = date('Y-m-d H:i:s', strtotime($yesterday . '+1 days')); $today = date('m-d-Y', strtotime($_POST['yesterday'])); $yesterday = date('Y-m-d H:i:s', strtotime($yesterday . '-1 days')); $f_today = $today; } } ?> <table border="1" align="center" width="100%"> <input type="hidden" name="yesterday" value=<?php echo $yesterday;?>> <input type="hidden" name="tomorrow" value=<?php echo $tomorrow;?>> <tr> <td align="center" width="20%"><?php echo $yesterday;?><input type="Submit" name="Submit" value="Yesterday"></td> <td align="center" width="60%"> <?php echo $f_today;?></td> <td align="center" width="20%"><input type="Submit" name="Submit" value="Tomorrow"><?php echo $tomorrow;?></td> </tr> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { //<!--- Form view ---> echo"<tr><td>query</td></tr>"; } else{ //<!--- Default view ---> "<tr><td>default</td></tr>"; } ?> </table> </form> </body> </html>
  3. 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; }
  4. I found this searching the internet searching for a way to convert compass heading to letters. I will look into php functions. Thanks
  5. 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.
  6. Thanks reqinix. I did try that, as well as several other formats. I am not sure why it wasn't taking it. See my last, I have it figured out.
  7. Finally! I switched one field to timestamp and generate on insert. I also switched the lastrain date to a string since it comes in as a string. It is inserting every time the page is hit. So getting closer.
  8. Error: INSERT INTO weather_data (datetime, temp, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( STR_TO_DATE('2022-01-24T20:52:00.000Z' , '%c-%e-%Y %T'), 49.1, 49.1, 29.959, 29.135, 47.16, 93, 274, 0.7, 1.1, 8.1, 0, 0.181, 0.181, 0.701, 49.043, STR_TO_DATE('2022-01-24T19:22:00.000Z', '%c-%e-%Y %T'), 57.62, 1 ) Incorrect datetime value: '2022-01-24T20:52:00.000Z' for function str_to_date WTF is up with dates in mysql? I haven't worked in it since version 5.* but I never had the problems I am having trying to insert. Any help would be greatly appreciated.
  9. Doh! Thank you and to all others who responded. I will look into parameterized queries.
  10. Quoting both datetime fields produces this error. Unknown column 'tempf' in 'field list'
  11. Error: INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( '1643040660', 48, 48, 30.053, 29.23, 45.79, 92, 299, 1.1, 1.1, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 43.8, 1 ) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':34:00.000Z, 43.8, 1 )' at line 22 This is the error converting string to datetime with strtotime.
  12. Error: INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( '2022-01-24T16:08:00.000Z', 48, 48, 30.056, 29.232, 45.79, 92, 331, 1.8, 2.2, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 47.22, 1 ) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':34:00.000Z, 47.22, 1 )' at line 22 This is what I get putting quotes around the DateTime.
  13. For the life of me I can not figure out the syntax error. I was thinking it was related to DateTime field. The data comes over as a string from the api, but converting to time doesn't work. The field in the database is DateTime. Any suggestions would be appreciated. The data from the api for reference. array(1) { [0]=> array(3) { ["macAddress"]=> string(17) "FA:F5:C2:98:6F:29" ["lastData"]=> array(27) { ["dateutc"]=> int(1642622880000) ["tempinf"]=> float(77.9) ["humidityin"]=> int(38) ["baromrelin"]=> float(29.956) ["baromabsin"]=> float(29.132) ["tempf"]=> float(77.2) ["battout"]=> int(1) ["humidity"]=> int(47) ["winddir"]=> int(247) ["windspeedmph"]=> float(5.6) ["windgustmph"]=> float(8.1) ["maxdailygust"]=> float(15.9) ["hourlyrainin"]=> int(0) ["eventrainin"]=> int(0) ["dailyrainin"]=> int(0) ["weeklyrainin"]=> int(0) ["monthlyrainin"]=> float(0.52) ["totalrainin"]=> float(48.862) ["solarradiation"]=> float(196.47) ["uv"]=> int(1) ["feelsLike"]=> float(76.83) ["dewPoint"]=> float(55.39) ["feelsLikein"]=> float(77.2) ["dewPointin"]=> float(50.2) ["lastRain"]=> string(24) "2022-01-12T02:50:00.000Z" ["tz"]=> string(15) "America/Chicago" ["date"]=> string(24) "2022-01-19T20:08:00.000Z" } ["info"]=> array(2) { ["name"]=> string(18) "My Weather Station" ["coords"]=> array(5) { ["coords"]=> array(2) { ["lon"]=> float(-197.65635809999999) ["lat"]=> float(38.6587316) } ["address"]=> string(44) "100 Main Street, Anytown, FL 08226, USA" ["location"]=> string(10) "Anytown" ["elevation"]=> float(214.7066497802734) ["geo"]=> array(2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-97.65635809999999) [1]=> float(30.6587316) } } } } } } I take the data from the array and assign it to variables. Notice the unsuccessful attempts to convert string to DateTime. <?php $_DateTime = $data[0]['lastData']['date']; //$_OldDateTime = $data[0]['lastData']['date']; //$_dateTime = strtotime($_OldDateTime); $_tempf = $data[0]['lastData']['tempf']; $_feelsLike = $data[0]['lastData']['feelsLike']; $_stationbarometer = $data[0]['lastData']['baromrelin']; $_sealevelbarometer = $data[0]['lastData']['baromabsin']; $_dewpoint = $data[0]['lastData']['dewPoint']; $_humidity = $data[0]['lastData']['humidity']; $_winddir = $data[0]['lastData']['winddir']; $_windspeed = $data[0]['lastData']['windspeedmph']; $_gust = $data[0]['lastData']['windgustmph']; $_maxdailygust = $data[0]['lastData']['maxdailygust']; $_hourlyrainrate = $data[0]['lastData']['hourlyrainin']; $_dailyrain = $data[0]['lastData']['dailyrainin']; $_weeklyrain = $data[0]['lastData']['weeklyrainin']; $_monthlyrain = $data[0]['lastData']['monthlyrainin']; $_totalrain = $data[0]['lastData']['totalrainin']; $_lastRain = $data[0]['lastData']['lastRain']; //$_OldlastRain = $data[0]['lastData']['lastRain']; //$_lastRain = strtotime($_OldlastRain); $_solar = $data[0]['lastData']['solarradiation']; $_battout = $data[0]['lastData']['battout']; // this is where we insert into the database $sql = "INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( $_DateTime, $_tempf, $_feelsLike, $_stationbarometer, $_sealevelbarometer, $_dewpoint, $_humidity, $_winddir, $_windspeed, $_gust, $_maxdailygust, $_hourlyrainrate, $_dailyrain, $_weeklyrain, $_monthlyrain, $_totalrain, $_lastRain, $_solar, $_battout )"; //mysqli_query($conn, $sql); if (mysqli_query($conn, $sql)) { echo "Success!"; //json_encode(array("statusCode"=>200)); } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?> Error message: Error: INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( 2022-01-24T15:40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, 1.3, 2.2, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 29.48, 1 ) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, ' at line 6
  14. I did try the print_r function I thought they were the same. I tried your suggestion and it worked! Thank you.
  15. I currently have a weather station and website that is shareware software. I get the weather data from Wunderground and inserts into the mysql database to power the site. I would like to use the weather station api and insert directly into the database, essentially by-passing Wunderground. I am able to read the api and see the array but unable to loop over the array and get the data I would like. The data I really want is in the lastData key/array This is the return from the api: array(1) { [0]=> array(3) { ["macAddress"]=> string(17) "FA:F5:C2:98:6F:29" ["lastData"]=> array(27) { ["dateutc"]=> int(1642622880000) ["tempinf"]=> float(77.9) ["humidityin"]=> int(38) ["baromrelin"]=> float(29.956) ["baromabsin"]=> float(29.132) ["tempf"]=> float(77.2) ["battout"]=> int(1) ["humidity"]=> int(47) ["winddir"]=> int(247) ["windspeedmph"]=> float(5.6) ["windgustmph"]=> float(8.1) ["maxdailygust"]=> float(15.9) ["hourlyrainin"]=> int(0) ["eventrainin"]=> int(0) ["dailyrainin"]=> int(0) ["weeklyrainin"]=> int(0) ["monthlyrainin"]=> float(0.52) ["totalrainin"]=> float(48.862) ["solarradiation"]=> float(196.47) ["uv"]=> int(1) ["feelsLike"]=> float(76.83) ["dewPoint"]=> float(55.39) ["feelsLikein"]=> float(77.2) ["dewPointin"]=> float(50.2) ["lastRain"]=> string(24) "2022-01-12T02:50:00.000Z" ["tz"]=> string(15) "America/Chicago" ["date"]=> string(24) "2022-01-19T20:08:00.000Z" } ["info"]=> array(2) { ["name"]=> string(18) "My Weather Station" ["coords"]=> array(5) { ["coords"]=> array(2) { ["lon"]=> float(-197.65635809999999) ["lat"]=> float(38.6587316) } ["address"]=> string(44) "100 Main Street, Anytown, FL 08226, USA" ["location"]=> string(10) "Anytown" ["elevation"]=> float(214.7066497802734) ["geo"]=> array(2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-197.65635809999999) [1]=> float(38.6587316) } } } } } } This is the code I have: <?php // Read JSON file $readjson = file_get_contents("https://api.") ; //Decode JSON $data = json_decode($readjson, true); //echo '<pre>' ; var_dump($data); ?> <h1>Weather Forecast for</h1> <table> <tr><th>Date</th><th>Max Temp</th><th>Min Temp</th><th>Precip</th><th>Wspd</th><th>Wgust</th><th>Cloud cover</th></tr> <?php foreach ($data as $key => $key_value) { echo "key is: " .$key. ", "."value is: " .$key_value; echo "<br>"; ?> <tr><td><?php echo $key_value; ?></td></tr> What I get for output:
×
×
  • 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.