Swoole\Form::muti_select PHP Method

muti_select() static public method

多选下拉选择菜单 $name 此select 的 name 标签 $array 要制作select 的数 $default 如果要设定默认选择哪个数据 就在此填入默认的数据的值 $self 设置为ture,option的值等于$value $attrArray html标签的熟悉 就是这个select的属性标签 例如 class="x1" $add_help 增加一个值为空的 请选择 项
static public muti_select ( $name, $option, $default = [], $self = null, $attrArray = null, $add_help = true )
    static function muti_select($name, $option, $default = array(), $self = null, $attrArray = null, $add_help = true)
    {
        $htmlStr = "<select name=\"{$name}\" id=\"{$name}\"";
        $htmlStr .= self::input_attr($attrArray) . ">\n";
        if ($add_help) {
            if ($add_help === true) {
                $htmlStr .= "<option value=\"\">" . self::$default_help_option . "</option>\n";
            } else {
                $htmlStr .= "<option value=\"\">{$add_help}</option>\n";
            }
        }
        foreach ($option as $key => $value) {
            if ($self) {
                $key = $value;
            }
            if (in_array($key, $default)) {
                $htmlStr .= "<option value=\"{$key}\" selected=\"selected\">{$value}</option>\n";
            } else {
                $htmlStr .= "<option value=\"{$key}\">{$value}</option>\n";
            }
        }
        $htmlStr .= "</select>\n";
        return $htmlStr;
    }

Usage Example

Example #1
0
 function add_module()
 {
     //\Swoole\Error::dbd();
     if (empty($_GET['id']) and empty($_POST)) {
         $gets['select'] = 'id,username,realname';
         $tmp = table('user')->gets($gets);
         $user = array();
         foreach ($tmp as $t) {
             $name = !empty($t['realname']) ? $t['realname'] : '';
             $user[$t['id']] = "{$name} [{$t['username']}]";
         }
         $form['name'] = \Swoole\Form::input('name');
         $form['intro'] = \Swoole\Form::text('intro');
         $form['owner_uid'] = \Swoole\Form::select('owner_uid', $user, '', null, array('class' => 'select2 select2-offscreen', 'style' => "width:100%"));
         $form['backup_uids'] = \Swoole\Form::muti_select('backup_uids[]', $user, array(), null, array('class' => 'select2 select2-offscreen', 'multiple' => "1", 'style' => "width:100%"), false);
         $tmp = table('project')->gets(array("order" => "id desc"));
         $project = array();
         foreach ($tmp as $t) {
             $project[$t['id']] = $t['name'];
         }
         $form['project_id'] = \Swoole\Form::select('project_id', $project, '', null, array('class' => 'select2 select2-offscreen', 'style' => "width:100%"));
         $this->assign('form', $form);
         $this->display();
     } elseif (!empty($_GET['id']) and empty($_POST)) {
         $id = (int) $_GET['id'];
         $module = table("module")->get($id)->get();
         $gets['select'] = '*';
         $tmp = table('user')->gets($gets);
         $user = array();
         foreach ($tmp as $t) {
             $name = !empty($t['realname']) ? $t['realname'] : '';
             $user[$t['id']] = "{$name} [{$t['username']}]";
         }
         $form['id'] = \Swoole\Form::hidden('id', $module['id']);
         $form['name'] = \Swoole\Form::input('name', $module['name']);
         $form['intro'] = \Swoole\Form::text('intro', $module['intro']);
         $form['owner_uid'] = \Swoole\Form::select('owner_uid', $user, $module['owner_uid'], null, array('class' => 'select2 select2-offscreen', 'style' => "width:100%"));
         $form['backup_uids'] = \Swoole\Form::muti_select('backup_uids[]', $user, explode(',', $module['backup_uids']), null, array('class' => 'select2 select2-offscreen', 'multiple' => "1", 'style' => "width:100%"), false);
         $tmp = table('project')->gets(array("order" => "id desc"));
         $project = array();
         foreach ($tmp as $t) {
             $project[$t['id']] = $t['name'];
         }
         $form['project_id'] = \Swoole\Form::select('project_id', $project, $module['project_id'], null, array('class' => 'select2 select2-offscreen', 'style' => "width:100%"));
         $this->assign('form', $form);
         $this->display();
     } elseif (!empty($_POST) and empty($_POST['id'])) {
         $in['name'] = trim($_POST['name']);
         $in['owner_uid'] = trim($_POST['owner_uid']);
         if (empty($in['owner_uid'])) {
             $in['owner_uid'] = $this->uid;
         }
         $in['project_id'] = trim($_POST['project_id']);
         $backup_uids = '';
         if (!empty($_POST['backup_uids'])) {
             $backup_uids = implode(',', $_POST['backup_uids']);
         }
         $in['backup_uids'] = $backup_uids;
         //            $in['backup_name'] = trim($_POST['backup_name']);
         $in['intro'] = trim($_POST['intro']);
         $c = table('module')->count(array('name' => $in['name']));
         if ($c > 0) {
             \Swoole\JS::js_goto("操作失败,已存在同名模块", "/setting/add_module/");
         } else {
             $id = table('module')->put($in);
             if ($id) {
                 \Swoole\JS::js_goto("操作成功", "/setting/add_module/");
                 \Swoole::$php->log->put("{$_SESSION['userinfo']['username']} add module success {$id} : " . print_r($in, 1));
             } else {
                 \Swoole\JS::js_goto("操作失败", "/setting/add_module/");
                 \Swoole::$php->log->put("{$_SESSION['userinfo']['username']} add module failed:  " . print_r($in, 1));
             }
         }
     } else {
         $id = (int) $_POST['id'];
         $in['name'] = trim($_POST['name']);
         $in['owner_uid'] = trim($_POST['owner_uid']);
         $in['project_id'] = trim($_POST['project_id']);
         $backup_uids = '';
         if (!empty($_POST['backup_uids'])) {
             $backup_uids = implode(',', $_POST['backup_uids']);
         }
         $in['backup_uids'] = $backup_uids;
         $in['intro'] = trim($_POST['intro']);
         $where['name'] = $in['name'];
         $where['where'][] = "id !={$id}";
         $c = table('module')->count($where);
         if ($c > 0) {
             \Swoole\JS::js_goto("操作失败,已存在同名模块", "/setting/module_list/");
         } else {
             $res = table('module')->set($id, $in);
             if ($res) {
                 \Swoole\JS::js_goto("操作成功", "/setting/module_list/");
                 \Swoole::$php->log->put("{$_SESSION['userinfo']['username']} modify module success {$id} : " . print_r($in, 1));
             } else {
                 \Swoole\JS::js_goto("操作失败", "/setting/module_list/");
                 \Swoole::$php->log->put("{$_SESSION['userinfo']['username']} modify module failed:  " . print_r($in, 1));
             }
         }
     }
 }
All Usage Examples Of Swoole\Form::muti_select