执行多个循环样式操作的更简单方法


Easier way to do multiple loop styled actions?

我是AJAX的新手,但在阅读和提出具体问题方面都得到了一些很好的帮助。这是针对php后端的。。。

我有很多这样的代码:

if(isset($_POST['shortTitle'])) {
    $userInput = ucwords($_POST['shortTitle']);
    if(trim($userInput) == "") { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `shortTitle` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
    $report_shortTitle = array();
    if($userInput == NULL) {
        $report_shortTitle['errorText_shortTitle'] = "This field cannot be left blank";
        $report_shortTitle['resultImg_shortTitle'] = "<img src='"./gfx/form_boo.gif'" class='"resultImg'" alt='"&#10008;'" title='"&#10008;'">";
    } else {
        $report_shortTitle['errorText_shortTitle'] = NULL;
        $report_shortTitle['resultImg_shortTitle'] = "<img src='"./gfx/form_yay.gif'" class='"resultImg'" alt='"&#10004;'" title='"&#10004;'">";
    }
    echo json_encode($report_shortTitle);
}
// groupName
if(isset($_POST['groupName'])) {
    $userInput = ucwords($_POST['groupName']);
    if(trim($userInput) == "") { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `groupName` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
    $report_groupName = array();
    if($userInput == NULL) {
        $report_groupName['errorText_groupName'] = "This field cannot be left blank";
        $report_groupName['resultImg_groupName'] = "<img src='"./gfx/form_boo.gif'" class='"resultImg'" alt='"&#10008;'" title='"&#10008;'">";
    } else {
        $report_groupName['errorText_groupName'] = NULL;
        $report_groupName['resultImg_groupName'] = "<img src='"./gfx/form_yay.gif'" class='"resultImg'" alt='"&#10004;'" title='"&#10004;'">";
    }
    echo json_encode($report_groupName);
}

所有这些都有效,然而,如果我能简单地用一个代码块来处理所有以相同风格操作的东西,那就太好了——从输入字段到DB插入的数据发布总是一致的——是shortTitle、groupName等。显然,

$stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);

行会有所不同,所以我想我需要不同的代码,例如"PDO::PARAM_STR,64"、PDO:"PARAM_INT,11"等,但这没关系。我如何才能做到这一点,使我只需要一位代码。我相信这一定是可能的,但我不确定如何实现这一点。感谢反馈!

您可以使用数组来获得可靠的部件,并使用foreach。

$names=['groupName','shortTitle'];

foreach($names as $name){
  if(isset($_POST[$name])) {
    $userInput = ucwords($_POST[$name]);
    if(trim($userInput) == "") { $userInput = NULL; }
    try {
      $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET '$name' = :userinput, `lastModified` = :time WHERE `appID` = :appid");
      $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
      $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
      $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
      $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
    $report_name = array();
    if($userInput == NULL) {
      $report_name['errorText_'.$name] = "This field cannot be left blank";
      $report_name['resultImg_'.$name] = "<img src='"./gfx/form_boo.gif'" class='"resultImg'" alt='"&#10008;'" title='"&#10008;'">";
    } else {
      $report_name['errorText_'.$name] = NULL;
      $report_name['resultImg_'.$name] = "<img src='"./gfx/form_yay.gif'" class='"resultImg'" alt='"&#10004;'" title='"&#10004;'">";
    }
    echo json_encode($report_name);
  }
}