我的数组的输出结果不是预期的 - php

时间:2014-05-29 10:53:45

标签: php arrays output

所以这个:

$status_choice = array();
$status_choice[] = "New Job";               
$status_choice[] = "Existing Case";

现在我用它了:

dropdown("Status:","status",$status_choice,"150px");

这基本上是输出html下拉列表的函数。但是,不是在下拉列表中出现的数组值,而是如下所示:

Dropdown showing two options, "e" and "x"

这里是我的dropdown()函数:

function dropdown($title,$name,$possible_values,$width="300px",$desc="",$onchange="") {

    global $dropdown_allow_nulls;

    $current_value = $GLOBALS["row_$name"];
    if ($current_value == "") { $current_value = $GLOBALS["form_$name"]; }

    if (is_write_lock()) {
        return dropdown_readonly($title, $name, $possible_values, $width, $desc, $onchange);
    }

    $myonchange = "";

    //if ($onchange != "") { $myonchange = "onChange=\"set_control_changed(this); $onchange\""; } else { $myonchange = "onChange=\"set_control_changed(this);\""; }

    if ($GLOBALS["myclass"] != $GLOBALS["myclass1"]) { $GLOBALS["myclass"] = $GLOBALS["myclass1"]; } else { $GLOBALS["myclass"] = $GLOBALS["myclass2"]; }
    $myclass = $GLOBALS["myclass"];

    print "<tr class=\"$myclass\">\n";
    print "<td style=\"padding-right: 10px;\">$title</td>\n";
    print "<td style=\"padding-bottom: 2px; padding-top: 1px;\">\n";

    print "<select name=\"$name\" $myonchange style=\"width: $width;\" id=\"form_$name\">\n";

    if (is_array($possible_values)) {

        if ($dropdown_allow_nulls == 1) {

            $key = "";
            $value = "";
            if ($key == $current_value) { $selected = "selected"; } else { $selected = ""; }

            print "<option value=\"$key\" $selected>$value</option>\n";

        }

        foreach ($possible_values as $myrow) {

            $key = $myrow[0];
            $value = $myrow[1];
            if ($key == $current_value) { $selected = "selected"; } else { $selected = ""; }

            print "<option value=\"$key\" $selected>$value</option>\n";

        }

    } else {

        // sql query

        $myquery = sql_query($possible_values);
        print sql_error();

        if ($dropdown_allow_nulls == 1) {

            $key = "";
            $value = "";
            if ($key == $current_value) { $selected = "selected"; } else { $selected = ""; }

            print "<option value=\"$key\" $selected>$value</option>\n";

        }

        while ($myrow = sql_fetch_row($myquery)) {

            $key = $myrow[0];
            $value = $myrow[1];
            if ($key == $current_value) { $selected = "selected"; } else { $selected = ""; }

            print "<option value=\"$key\" $selected>$value</option>\n";

        }

    }

    print "</select>\n";

    if ($desc) {
        print "<span style=\"margin-left: 10px; color: #888888;\">$desc</span>\n";
    }

    print "</td>\n";
    print "</tr>\n";

} 

3 个答案:

答案 0 :(得分:1)

    foreach ($possible_values as $myrow) {

        $key = $myrow[0];
        $value = $myrow[1];

此时,$myrow是一个字符串,首先是“新作业”,然后是“现有案例”。因此,$key设置为NE,而$value设置为ex

使用:

foreach($possible_values as $key=>$value) {

代替。

答案 1 :(得分:0)

您正在尝试获取值的字符数组。您需要获取主数组的键和值部分而不是值(将其更改为字符数组)。更改以下行:

...
foreach ($possible_values as $key=>$value) {

       // $key = $myrow[0];
       // $value = $myrow[1];
        if ($key == $current_value) { $selected = "selected"; } else { $selected = ""; }

        print "<option value=\"$key\" $selected>$value</option>\n";

    }
...

答案 2 :(得分:0)

您的$myrow[1]将获得$ myrow的第二个字符,将$value = $myrow[1]更改为$value = $myrow,或更改以下代码

foreach ($possible_values as $myrow) {

    $key = $myrow[0];
    $value = $myrow[1];
    if ($key == $current_value) { $selected = "selected"; } else { $selected = ""; }

    print "<option value=\"$key\" $selected>$value</option>\n";

}

使用以下内容尝试

foreach ($possible_values as $key => $value) {
    $selected = $key == $current_value ? 'selected' : '';
    print "<option value=\"$key\" $selected>$value</option>\n";
}