Salut à tous,
J'ai une station prend des mesures grâce à plusieurs sondes. Chaque sonde a un seuil critique.
Si 3 ou 4 sondes mesurent une valeur critique, je dois insérer dans ma tables 'notifications' 3 ou 4 lignes.
Avant la requete MYSQl, j'ai un array qui contient les mesures
Je pourrais parcourir mon array et faire 3 requetes
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 // $content Array ( [0] => Array ( [id_collection] => 309001 [id_station] => 43 [id_sensor] => 154 [messageFR_db] => [1: 15cm] La mesure 24kPa doit se situer entre 30.00kPa et 60.00kPa [messageEN_db] => [1: 15cm] The measure 24kPa must be between 30.00kPa and 60.00kPa [messageFR] => La sonde [1: 15cm] est déclenchée [messageEN] => the sensor [1: 15cm] is trigged [operator] => <> ) [1] => Array ( [id_collection] => 309001 [id_station] => 43 [id_sensor] => 155 [messageFR_db] => [2: 25cm] La mesure 17kPa ne doit pas se situer entre 10.00kPa et 60.00kPa [messageEN_db] => [2: 25cm] The measure 17kPa must not be between 10.00kPa and 60.00kPa [messageFR] => La sonde [2: 25cm] est déclenchée [messageEN] => the sensor [2: 25cm] is trigged [operator] => >< ) [2] => Array ( [id_collection] => 309001 [id_station] => 43 [id_sensor] => 158 [messageFR_db] => [Pluie] la mesure 0mm est en dessous du seuil 30.00mm [messageEN_db] => [Pluie] The measure 0mm is below the thresold 30.00mm [messageFR] => La sonde [Pluie] est déclenchée [messageEN] => the sensor [Pluie] is trigged [operator] => > ) )
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 if(count($content) > 0){ /* * SAVE ALARM INTO THE notifications TABLE */ foreach($content) as $k => $v{ // Prepare the MySQL request $sql_insert_notifications = 'INSERT into notifications ( id_notification, stations_id_station, sensors_id_sensor, collections_id_collection, notification_date, notification_message_fr, notification_message_en ) VALUES ( "", "'.$v['id_station'].'", "'.$v['id_sensor'].'", "'.$v['id_collection'].'", "'.gmdate("Y-m-d H:i:s").'", "'.$v['messageFR_db'].'", "'.$v['messageEN_db'].'" )'; if (!$_connect->query($sql_insert_notifications) ) { if($debug) printf("Erreur : %s\n", $connect->error); }else{ if($debug) echo "<p>Alarm inserted</p>"; } } }
ca irait très bien comme cela.
Mais je me demandais s'il était possible de faire qu'une fois
Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part $_connect->query($sql_insert_notifications)
En rédirigeant ce message, j'ai vu ceci 'multi_query'
Je pourrais donc modifier comme ceci
Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 $sql_insert_notifications = "INSERT INTO notifications ( id_notification, stations_id_station, sensors_id_sensor, collections_id_collection, notification_date, notification_message_fr, notification_message_en ) VALUES ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en'), ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en'), ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en' )"; if ($_connect->multi_query($sql_insert_notifications) === TRUE) { echo "New records inserted successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error;
La question que je me pose, est comment je peux construire cette partie, à partir de mon array $content, sachant que le nombre d'insert peu différer
Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en'), ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en'), ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en' )
Je ne peux pas faire un foreach entre VALUE et le dernier "
Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4VALUES ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en'), ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en'), ('', 'stations_id_station', 'sensors_id_sensor', 'collections_id_collection', 'notification_date', 'notification_message_fr','notification_message_en' )"





Répondre avec citation






Consultez nos FAQ : 
Partager