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

时间:2015-03-27 15:27:25

标签: php

我在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);
}

这一切都有效,但是如果我能够简单地将一个代码块用于以相同样式操作的所有内容将会很好 - 从输入字段一直到数据库插入的数据发布总是一致的 - 是shortTitle,groupName等。显然是

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

行会有所不同所以我想我需要不同的代码片段,对于&#34; PDO :: PARAM_STR,64&#34;,PDO :: PARAM_INT,11&#34;等等但是没关系。我该如何工作,以便我只需要一点代码。我确信它一定是可能的,但我不确定如何实现这一目标。反馈赞赏!

1 个答案:

答案 0 :(得分:1)

您可以将数组用于可靠的部分并使用foreach。

$ names = [&#39; groupName&#39;,&#39; shortTitle&#39;];

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);
  }
}