1

我遇到了最严重的错误......下面的代码按预期运行并按预期访问if语句的两半(我可以说是因为我将“TRUE”和“FALSE”都添加到错误日志中并得到它们在导致它们的条件的输出上方添加)。

但是当我访问 的TRUE部分时if,由于某种原因它没有回显。我知道代码部分没有被跳过(因为error_log("TRUE");仍然运行)。

代码摘要:

遍历状态列表,输出 HTML 标记的一半(有条件地添加中间位),然后输出结束的一半。

function createStateOptions($country, $state) {
    $sql = "SELECT * FROM tbl_state WHERE disabled != true AND deleted != true AND country = :country ORDER BY state ASC";
    $result = executeSQL($sql, array(':country' => $country));
    if (is_array($result)) {
        foreach ($result as $record) {
            echo '<option value="' . htmlentities($record["state"], ENT_QUOTES) . '" ';
            if($state == $record["state"]) {
                echo " selected";
                error_log("TRUE");
            } else {
                error_log("FALSE");
            }
            echo '>' . htmlentities($record["state"], ENT_QUOTES) . '</option>';
            error_log("'".$state."' == '".$record["state"]."'"); 
        }
    }
}
4

1 回答 1

0

尝试构建一个字符串,然后在最后回显

function createStateOptions($country, $state) {
  $output = '';
  $sql = "SELECT * FROM tbl_state WHERE disabled != true AND deleted != true AND country = :country ORDER BY state ASC";
  $result = executeSQL($sql, array(':country' => $country));
  if (is_array($result)) {
    foreach ($result as $record) {
        $output = '<option value="' . htmlentities($record["state"], ENT_QUOTES) . '" ';
        if($state == $record["state"]) {
            $output .= " selected";
            error_log("TRUE");
        } else {
            error_log("FALSE");
        }
        $output .= '>' . htmlentities($record["state"], ENT_QUOTES) . '</option>';
        error_log("'".$state."' == '".$record["state"]."'"); 
        echo $output;
    }
  }
}
于 2012-07-26T04:44:37.270 回答