使用$ _POST循环删除键值对

时间:2014-10-22 15:10:52

标签: php

我有一个简单的问题,但我没有看到解决方案。我能够更新和/或插入键值对。但是如果$ _POST数组中不再存在meta_shopping_value,我想删除一个键值对。

我有一个简单的用户界面,用户可以编辑他的订单。我想我需要以下逻辑:

if $check_key not in $_POST -> delete
if $_POST == $check_key  -> update
if $_POST not in $check_key  -> insert



 function update_furniture_key_value_db($params) {
    foreach ($params as $key => $val) {
            //CHECK if key value pair exists UPDATE
            $check_key = $exhibitor_data->read_user_key_data($key);
            if (isset($check_key->meta_shopping_value)) {
              //echo ' UPDATE ' . $check_key->meta_shopping_value . '<br/>';
              echo $exhibitor_data->update_user_key_data($key, $val) . '<br/>';
//DELETE if not 
            } else {//INSERT
                // 'INSERT query<br/>';
               echo $exhibitor_data->create_user_key_data($key, $val) . '<br/>';
            }
        }
    }

    update_furniture_key_value_db($_POST);

我试过这个功能

  function update_furniture_key_value_db($params) {
        foreach ($params as $key => $val) {
                //CHECK if key value pair exists UPDATE
               $check_key = $exhibitor_data->read_user_key_data($key);
        if (isset($check_key->meta_shopping_value) && ($check_key == $key)) {
           //echo $key . ' = ' . $check_key->meta_shopping_value . '<br/>';
            echo $exhibitor_data->update_user_key_data($key, $val) . '<br/>';
        } elseif (isset($check_key->meta_shopping_value)) {
              echo $exhibitor_data->delete_user_key_data($key);
            //echo 'DELETE';
        } else {// INSERT
           // echo 'INSERT query<br/>';
            echo $exhibitor_data->create_user_key_data($key, $val) . '<br/>';
        }
            }
        }

        update_furniture_key_value_db($_POST);

1 个答案:

答案 0 :(得分:0)

好的,我想出了这个解决方案,而不是最完美的解决方案,但我现在正在做这个工作

表格结构是:

meta_id wp_user_id meta_shopping_key meta_shopping_value

1 / from $ params只在tmpArrayPost中放入meta_shopping_key值 2 /检查存储的键/值对是否在数组中tmpArrayPost(!in_array($ stored_value-&gt; meta_shopping_key,$ tmpArrPost)) 3 /如果没有执行删除查询

      $exhibitor_data = new ExhibitorData($current_user->ID);
            foreach($params as $param => $value){
                $tmpArrPost[] .= $param;
            }   
       $stored_key_values = $exhibitor_data->read_user_all_meta_data($current_user->ID);
        foreach ($stored_key_values as $key => $stored_value) {
                    //CHECK IF STORED KEY IS ALSO SELECTED IN $_POST/$params
                    if (!in_array($stored_value->meta_shopping_key, $tmpArrPost)) {
                        $exhibitor_data->delete_user_key_data($stored_value->meta_shopping_key);
                    } 
                }
//now continue
    foreach ($params as $key => $val) {
            //CHECK for UPDATE or INSERT
            $check_key = $exhibitor_data->read_user_key_data($key);
            if (isset($check_key->meta_shopping_value)) {
               //update
                echo $exhibitor_data->update_user_key_data($key, $val) . '<br/>';
            } else {//ELSE INSERT
                //echo 'INSERT query<br/>';
                echo $exhibitor_data->create_user_key_data($key, $val) . '<br/>';
            }
        }