Help you to write the output code of the execution result of a piece of code

Source: Internet
Author: User
The following is a piece of php code. The main function is to operate the database, but to operate the database according to the $ new_data array (array 1, therefore, the $ new_data array (array 1) file is shown below for multiple database operations each time. The result to be completed is that the program can be judged after the loop execution is completed... the following is a piece of php code. The main function is to operate databases, but database operations are performed according to the $ new_data array (array 1). Therefore, multiple database operations are performed each time.
The following is the content of the $ new_data array (array 1) file.

The result is that after the program is executed cyclically, it can determine which database operation failed and which one succeeded. The program will execute multiple database operations in the loop, and the output result is all successfully output {"OK": true}, all failed {"OK ": false} partially failed (for example, {"del": false, "del_err": $ u_id | $ tasw_id, "up": false, "up_err ": $ u_id | $ tasw_id}
Please note that the operation of deleting, updating, and inserting the database will be performed multiple times in the loop. Of course, you can rewrite the code, but the array format is fixed. $ new_data array (array 1) after $ u_id = $ res ['U _ id']; is executed, the following $ res array (array 2) is generated to facilitate database operations.

Foreach ($ new_data as $ key) {$ res = array (); foreach ($ key as $ itm) {foreach ($ itm as $ it => $ valu) {$ res [str2 ($ it)] = $ valu ;}}$ u_id = $ res ['U _ id']; $ res ['tasw _ id'] = $ tasw_id; if ($ tasw_p = 'X') {// perform the delete operation $ tasw_u_id = substr ($ e [0], 1, strlen ($ e [0]); if (! Db_del ($ tasw_id, $ tasw_u_id) {$ tasw_err ['d _ err '] = false;} else {$ tasw_err ['d _ OK'] = true ;}} $ chk_u_id = $ db-> FirstValue ("select count (*) FROM {$ trauma} WHERE u_id = '{$ u_id}' and t_id = '{$ tasw_id}' "); if ($ chk_u_id> 0) {// update unset ($ res ['U _ id']); if (! $ Db-> AutoExecute ($ traumatic, $ res, 'update', "u_id = '{$ u_id}' and tree_id = '{$ tasw_id }'")) {$ tasw_err ['U _ err'] = false;} else {$ tasw_err ['U _ OK '] = true;} else {// insert if (! $ Db-> AutoExecute ($ traumatic, $ res) {$ tasw_err ['I _ err'] = false ;} else {$ tasw_err ['I _ OK'] = true ;}}}

Array 1

Array(    [VLZC9] => Array        (            [0] => Array                (                    [i] => VLZC9                    [^] => ASRWE                    [g] => m                    [l] => 12                )            [1] => Array                (                    [i] => VLZC9                    [p] => 2                )        )    [FQE10] => Array        (            [0] => Array                (                    [i] => FQE10                    [^] => ASRWE                    [g] => f                    [s] => VLZC9                    [l] => 12                )            [1] => Array                (                    [i] => FQE10                    [p] => 1                )        )    [ASRWE] => Array        (            [0] => Array                (                    [i] => ASRWE                    [m] => FQE10                    [f] => VLZC9                )        )    [WEESA] => Array        (            [0] => Array                (                    [i] => WEESA                    [x] =>                )        ))

Array 2

Array(    [u_id] => VLZC9    [s_s] => ASRWE    [g] => m    [l] => 12    [p] => 2)Array(    [u_id] => FQE10    [s_s] => ASRWE    [g] => f    [s] => VLZC9    [l] => 12    [p] => 1)Array(    [u_id] => ASRWE    [m] => FQE10    [f] => VLZC9)

Reply content:

The following is a piece of php code. The main function is to operate databases, but database operations are performed according to the $ new_data array (array 1). Therefore, multiple database operations are performed each time.
The following is the content of the $ new_data array (array 1) file.

The result is that after the program is executed cyclically, it can determine which database operation failed and which one succeeded. The program will execute multiple database operations in the loop, and the output result is all successfully output {"OK": true}, all failed {"OK ": false} partially failed (for example, {"del": false, "del_err": $ u_id | $ tasw_id, "up": false, "up_err ": $ u_id | $ tasw_id}
Please note that the operation of deleting, updating, and inserting the database will be performed multiple times in the loop. Of course, you can rewrite the code, but the array format is fixed. $ new_data array (array 1) after $ u_id = $ res ['U _ id']; is executed, the following $ res array (array 2) is generated to facilitate database operations.

Foreach ($ new_data as $ key) {$ res = array (); foreach ($ key as $ itm) {foreach ($ itm as $ it => $ valu) {$ res [str2 ($ it)] = $ valu ;}}$ u_id = $ res ['U _ id']; $ res ['tasw _ id'] = $ tasw_id; if ($ tasw_p = 'X') {// perform the delete operation $ tasw_u_id = substr ($ e [0], 1, strlen ($ e [0]); if (! Db_del ($ tasw_id, $ tasw_u_id) {$ tasw_err ['d _ err '] = false;} else {$ tasw_err ['d _ OK'] = true ;}} $ chk_u_id = $ db-> FirstValue ("select count (*) FROM {$ trauma} WHERE u_id = '{$ u_id}' and t_id = '{$ tasw_id}' "); if ($ chk_u_id> 0) {// update unset ($ res ['U _ id']); if (! $ Db-> AutoExecute ($ traumatic, $ res, 'update', "u_id = '{$ u_id}' and tree_id = '{$ tasw_id }'")) {$ tasw_err ['U _ err'] = false;} else {$ tasw_err ['U _ OK '] = true;} else {// insert if (! $ Db-> AutoExecute ($ traumatic, $ res) {$ tasw_err ['I _ err'] = false ;} else {$ tasw_err ['I _ OK'] = true ;}}}

Array 1

Array(    [VLZC9] => Array        (            [0] => Array                (                    [i] => VLZC9                    [^] => ASRWE                    [g] => m                    [l] => 12                )            [1] => Array                (                    [i] => VLZC9                    [p] => 2                )        )    [FQE10] => Array        (            [0] => Array                (                    [i] => FQE10                    [^] => ASRWE                    [g] => f                    [s] => VLZC9                    [l] => 12                )            [1] => Array                (                    [i] => FQE10                    [p] => 1                )        )    [ASRWE] => Array        (            [0] => Array                (                    [i] => ASRWE                    [m] => FQE10                    [f] => VLZC9                )        )    [WEESA] => Array        (            [0] => Array                (                    [i] => WEESA                    [x] =>                )        ))

Array 2

Array(    [u_id] => VLZC9    [s_s] => ASRWE    [g] => m    [l] => 12    [p] => 2)Array(    [u_id] => FQE10    [s_s] => ASRWE    [g] => f    [s] => VLZC9    [l] => 12    [p] => 1)Array(    [u_id] => ASRWE    [m] => FQE10    [f] => VLZC9)


  $ Valu) {$ res [str2 ($ it)] = $ valu ;}$ u_id = $ res ['U _ id']; $ res ['tasw _ id'] = $ tasw_id; $ tasw_err = array (); if ($ tasw_p = 'X ') {// perform the delete operation $ tasw_u_id = substr ($ e [0], 1, strlen ($ e [0]); if (! Db_del ($ tasw_id, $ tasw_u_id) {$ tasw_err ['del '] = false; $ tasw_err ['del _ err'] = sprintf (' % d | % d ', $ u_id, $ tasw_id) ;}$ chk_u_id = $ db-> FirstValue ("select count (*) FROM {$ trauma} WHERE u_id = '{$ u_id}' and t_id = '{$ tasw_id}' "); if ($ chk_u_id> 0) {// update unset ($ res ['U _ id']); if (! $ Db-> AutoExecute ($ traumatic, $ res, 'update', "u_id = '{$ u_id}' and tree_id = '{$ tasw_id }'")) {$ tasw_err ['up'] = false; $ tasw_err ['U _ err'] = sprintf ('% d | % d', $ u_id, $ tasw_id );}} else {// insert if (! $ Db-> AutoExecute ($ traumatic, $ res) {$ tasw_err ['in'] = false; $ tasw_err ['I _ err'] = sprintf (' % d | % d', $ u_id, $ tasw_id) ;}} if (! Empty ($ tasw_err) {$ result [] = $ tasw_err ;}} if (empty ($ result )) {$ result = array ('OK' => true,);} elseif (count ($ result) = count ($ new_data )) {$ result = array ('OK' => false,);} die (json_encode ($ result ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.