The series of questions about the inconsistent density of PHP organization data are limited. I encountered a problem when I was doing a small project. I hope you can help me. The cfg_hisdata table is used to store the historical device parameter information, including three key fields: TimeH (used to store 6-digit year-month-day data) and TimeL (used to store 6-digit hour-minute-second data ), paramContent (used to store device parameters ). The storage rules are a series of questions about inconsistent PHP organization density data.
The younger brother has limited capabilities and has encountered a problem when working on a small project. I hope you can help me.
The cfg_hisdata table is used to store the historical device parameter information, including three key fields: TimeH (used to store 6-digit year-month-day data) and TimeL (used to store 6-digit hour-minute-second data ), paramContent (used to store device parameters ).
The storage rules are stored every hour. In addition, they are stored once (accurate to seconds) when the parameters change ).
So for me, the data density I get from the database is not even. I need to organize the data in minutes (ignore seconds) from the query date 0 to the 24, and then pass it to the foreground.
My previous solutions are as follows:
PHP code
// Complete the obtained date and calculate the number of days. multiply the number of days by 1440*60 to get the number of seconds $ dateStart = 20120420; // query the start date $ dateEnd = 20120420; // query end date $ day = (strtotime ($ dateEnd)-strtotime ($ dateStart)/3600/24 + 1; // calculate the query days $ ltime = date ('ymdhis ', mktime (0, 0, 0, substr ($ dateStart, 4, 2), substr ($ dateStart, 6, 2), substr ($ dateStart, 0, 4 ))); // Generate the query start date 00:00 for ($ I = 0; $ I <1440*60 * $ day; $ I ++) // cyclically generate an array in seconds as value and use it as the value {$ fullTime [] = $ ltime; $ ltime = date ('ymdhis ', strtotime ("$ ltime + 1 seconds"); // Time + 1 second} $ secArr = array_fill_keys ($ fullTime ,"");
The preceding figure shows that the data precision is second, so an array whose key is second is constructed.
After
PHP code
While ($ result_va = mysqli_fetch_assoc ($ query) {$ temp_data = $ result_va ['paramcontent']; $ temp_time = $ result_va ['timeh']. $ result_va ['timer']; $ secArr [$ temp_time] = (float) $ temp_data; if ($ cache_d) // prevent the starting position of the rewrite array {$ secArr = array_fill ($ cache_t, (int) $ temp_time-$ cache_t, $ cache_d ); // fill in the null value in the current valid value array with the previous valid value} // use the current valid value and time as the filling value for backup $ cache_d = $ temp_data; $ cache_t = $ temp_time ;}
That is to say, when traversing the query results, put them into the seconds-level array that was previously organized based on the time scale. when traversing the next record, fill in the idle values between them with the values of the previous record.
After the array is organized, I plan to traverse the second-level array at an interval of 60, and put it into the new array in minutes for final output.
However, I feel that my thoughts are too cumbersome and too many cycles are too inefficient.
I am not familiar with development, and the level belongs to the entry level. Thank you for your advice.
------ Solution --------------------
That is to say, the data written every minute is the last data written in this minute. is that correct?
The process becomes simple:
Query string writing
$ SQL = <SQL
SELECT * FROM table where TimeH = '$ datestart' and
Timel in (select max (timel) from table where TimeH = '$ datestart' group by left (timel, 4 ))
SQL;
A slight modification can be made across days
After reading the data, you only need to judge whether it is consecutive for minutes.