Cainiao asks PHP to retrieve MYSQL data and then invert the order of mysql data
Php code:
$ Res_ip = mysql_query ("select * from ip order by id DESC limit 0, 5"); while ($ row_ip = mysql_fetch_array ($ res_ip )) {$ ip_ip [] = intval ($ row_ip ['IP']); $ time_ip [] = date ($ row_ip ['Time']);} $ data_ip = array ("name" => "IP traffic", "data" => $ ip_ip); $ data_ip = json_encode ($ data_ip ); $ times_ip = json_encode ($ time_ip); $ times_ip = str_replace ('2017-', '', $ times_ip );
Result:
["01-25","01-25","01-24","01-23","01-22"]
[{"name":"IP\u6d41\u91cf","data":[12632220,12735020,127350,2213488,2348888]}]
Expected results:
["01-22","01-23","01-24","01-25","01-26"]
[{"name":"IP\u6d41\u91cf","data":[2348888,2213488,127350,12735020,12632220]}]
Note: This is equivalent to putting the latest 5 data records into the inverted structure. Normally, the 5 latest data records are: 5 4 3 2 1. what you want is 1 2 3 4 5. 5. The latest data.
Thank you!
Reply to discussion (solution)
Sort by id! Decrease or increase! You should be increasing!
Select * from ip order by id ASC limit 0, 5?
Changing id desc to id asc won't work!
Id desc is designed to get the latest data, while id asc is the oldest data.
Use array_reverse to transpose an array
$ Data_ip = array ("name" => "IP traffic", "data" => array_reverse ($ ip_ip); $ data_ip = json_encode ($ data_ip ); $ times_ip = json_encode (array_reverse ($ time_ip); $ times_ip = str_replace ('2017-', '', $ times_ip );
Id sorting should be enough. should this be written in chronological order?
Id sorting should be enough. should this be written in chronological order?
ASC does not work. The old data is retrieved.
Changing id desc to id asc won't work!
Id desc is designed to get the latest data, while id asc is the oldest data.
Use array_reverse to transpose an array
$ Data_ip = array ("name" => "IP traffic", "data" => array_reverse ($ ip_ip); $ data_ip = json_encode ($ data_ip ); $ times_ip = json_encode (array_reverse ($ time_ip); $ times_ip = str_replace ('2017-', '', $ times_ip );
This method is feasible. thank you.