select元素的PHP函数 - 显示不存在的值

时间:2016-07-24 19:29:17

标签: php

我在PHP中创建了这个函数,用于在select元素中显示值

if(!function_exists("StatusSelect")) {
    function StatusSelect($selected = array(), $type, $behaviour = '') {
        global $mysqli;

        $val = '';
        $ret = '';

        $list = Statuses($type, $behaviour);
        foreach($list["results"] as $item) {
            $ret.= '<option value="'.$item["name"].'"';
            if(in_array($item["name"], $selected) or $item["default"] == 'Y') {
                $ret.= ' selected="selected"';
            }
            $ret.= '>'.($item["display"] ? $item["display"] : $item["name"]).'</option>';
        }

        return $ret;
    }
}

它调用一个名为Statuses的函数,它返回一个项目循环

所以我把这个函数称为:

echo '<select name="status" class="form-control required">';
echo StatusSelect(array('1'), "test");
echo '</select>';

并且它工作正常,并将正确的值设置为selected但我想要一种方法仍然显示一个值,如果它不再存在于循环中,但它包含在$selected数组

以此为例,如果我有以下值:

1
2
3
4
调用Statuses函数

时在循环中返回的

且值5位于$selected数组

我希望元素看起来像:

<select name="status" class="form-control required">
<option value="5" selected>5</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

但目前它只是:

<select name="status" class="form-control required">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>

Statuses功能:

function Statuses($type, $behavior='') {
    global $conn;

    $results=array();
    $sql="SELECT * from statuses where type = '".$type."' ";
    if($behavior != '') {
        $sql.="and behavior = '".$behavior."' ";
    }
    $sql.="ORDER by list_order, name ASC ";
    $rs=mysql_query($sql,$conn);
    while($result=mysql_fetch_array($rs)) {
        $results[] = $result;
    }
    return array("results" => $results);
}

2 个答案:

答案 0 :(得分:0)

您可以将功能Statuses($type, $behaviour)更改为以下内容:

function Statuses($type, $behavior='', $selected = null) {
    global $conn;

    $results=array();
    $sql="SELECT * from statuses where type = '".$type."' ";
    if($behavior != '') {
        $sql.="and behavior = '".$behavior."' ";
    }
    $sql.="ORDER by list_order, name ASC ";
    $rs=mysql_query($sql,$conn);
    while($result=mysql_fetch_array($rs)) {
        $results[] = $result;
    }
    if (null !== $selected && is_array($selected)) {
        foreach($selected as $val) {
            $results[] = array('name' => $val, 'default' => 'N', 'display' => $val);
        }
    }
    return array("results" => $results);
}

或者,您可以更改StatusSelect(),如下所示:

function StatusSelect($selected = array(), $type, $behaviour = '') {
    global $mysqli;

    $val = '';
    $ret = '';

    $list = Statuses($type, $behaviour);
    $found = false;
    foreach($list["results"] as $item) {
        $ret.= '<option value="'.$item["name"].'"';
        if(in_array($item["name"], $selected) or $item["default"] == 'Y') {
            $found = true;
            $ret.= ' selected="selected"';
        }
        $ret.= '>'.($item["display"] ? $item["display"] : $item["name"]).'</option>';
    }
    if (!$found) {
        foreach($selected as $val) {
            $ret = '<option value="' . $val . '" selected="selected">' . $val . '</option>' . $ret;
        }
    }

    return $ret;
 }

tested =)。输出:

<option value="5" selected="selected">5</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>

答案 1 :(得分:0)

我建议创建一个中间函数,它将使用$list中的值更新$selected,然后将其发送以生成html:

<?php

function Statuses($type, $behaviour) {
    // just return some static data:
    return array(
        "results" => 
        array(
            array("name" => "1", "default" => "N"),
            array("name" => "2", "default" => "N"),
            array("name" => "3", "default" => "N"),
            array("name" => "4", "default" => "N"),
        ),
   );
}

// read Statuses() and update with synthetic values from $selected
function StatusesWithSelected($type, $behaviour, $selected) {
    $statuses = Statuses($type, $behaviour);
    $newStatuses = array_diff($selected, array_map(function($x){return $x["name"];}, $statuses["results"]));
    foreach ($newStatuses as $s){
        $statuses["results"][] = array(
            "name" => $s,
            "default" => "Y",
            );
    }
    return $statuses;
}
if(!function_exists("StatusSelect")) {
    function StatusSelect($selected = array(), $type, $behaviour = '') {
        global $mysqli;

        $val = '';
        $ret = '';

        $list = StatusesWithSelected($type, $behaviour, $selected);
        foreach($list["results"] as $item) {
            $ret.= '<option value="'.$item["name"].'"';
            if($item["default"] == 'Y') {
                $ret.= ' selected="selected"';
            }
            $ret.= '>'.(isset($item["display"]) ? $item["display"] : $item["name"]).'</option>'; // I added isset here to prevent Notices
        }

        return $ret;
    }
}

echo StatusSelect(array('4', '5'), "test");

Tested。输出:

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5" selected="selected">5</option>