function addBunchBasicSubItems($subItem) { $count = count(explode(">>><<<", $subItem["subItemName"])); foreach ($subItem as $key => $value) { if ($key != "parentId") { $subItem[$key] = explode(">>><<<", $value); } } $result = array(); $addArr = array(); for ($i = 0; $i < $count; $i++) { $addArr["subItemId"] = "basic-sub-" . date("YmdHis") . str_pad(rand(0, 9999), 4, rand(0, 9), STR_PAD_LEFT); // multiply 1000, forced convert price into integer. $addArr["subItemName"] = $subItem["subItemName"][$i]; $addArr["subItemUnit"] = $subItem["subItemUnit"][$i]; $addArr["mainMaterialPrice"] = $subItem["mainMaterialPrice"][$i]; $addArr["auxiliaryMaterialPrice"] = $subItem["auxiliaryMaterialPrice"][$i]; $addArr["manpowerPrice"] = $subItem["manpowerPrice"][$i]; $addArr["machineryPrice"] = $subItem["machineryPrice"][$i]; $addArr["lossPercent"] = $subItem["lossPercent"][$i]; $addArr["parentId"] = $subItem["parentId"]; $addArr["manpowerCost"] = $subItem["manpowerCost"][$i]; $addArr["mainMaterialCost"] = $subItem["mainMaterialCost"][$i]; $addArr["remark"] = $subItem["remark"][$i]; $addArr["workCategory"] = $subItem["workCategory"][$i]; $info = addBasicSubItem($addArr); if ($info["status"] != "successful") { array_push($result, $addArr["subItemName"]); } } if (count($result) <= 0) { return array('status' => 'successful', 'errMsg' => ''); } else { $result = implode(",", $result); return array('status' => 'failing', 'errMsg' => $result . "未添加成功!"); } }
if ($key != "parentId") { $subItem[$key] = explode(">>><<<", $value); } } $result = array(); $addArr = array(); for ($i = 0; $i < $count; $i++) { $addArr["subItemId"] = "basic-sub-" . date("YmdHis") . str_pad(rand(0, 9999), 4, rand(0, 9), STR_PAD_LEFT); // multiply 1000, forced convert price into integer. $addArr["subItemName"] = $subItem["subItemName"][$i]; $addArr["subItemUnit"] = $subItem["subItemUnit"][$i]; $addArr["mainMaterialPrice"] = $subItem["mainMaterialPrice"][$i]; $addArr["auxiliaryMaterialPrice"] = $subItem["auxiliaryMaterialPrice"][$i]; $addArr["manpowerPrice"] = $subItem["manpowerPrice"][$i]; $addArr["machineryPrice"] = $subItem["machineryPrice"][$i]; $addArr["lossPercent"] = $subItem["lossPercent"][$i]; $addArr["parentId"] = $subItem["parentId"]; $addArr["cost"] = $subItem["cost"][$i]; $addArr["remark"] = $subItem["remark"][$i]; $info = addBasicSubItem($addArr); $info = json_decode($info, true); if ($info["status"] != "successful") { array_push($result, $addArr["subItemName"]); } } if (count($result) <= 0) { echo json_encode(array('status' => 'successful', 'errMsg' => '')); } else { $result = implode(",", $result); echo json_encode(array('status' => 'failing', 'errMsg' => $result . "未添加成功!")); }
<?php include_once "conn.php"; include_once "subitemDB.php"; $action = strtolower($_REQUEST["action"]); $res = array('status' => 'successful', 'errMsg' => ''); switch ($action) { case "add": $res = addBasicSubItem($_POST); break; case "addsome": $res = addBunchBasicSubItems($_POST); break; case "edit": $res = editBasicSubItem($_POST); break; case "delete": $res = deleteBasicSubItem($_POST['subItemId']); break; case "get": $res = getBasicSubItems($_GET['parentId']); break; default: throw new Exception("unknown action:" . $action); } if (is_array($res) && empty($res)) { // todo } else { if (!$res) { $res = array('status' => 'successful', 'errMsg' => ''); }