Codeigniter 下拉列表重复

时间:2021-06-16 18:28:14

标签: php sql laravel codeigniter frameworks

总部下有几所学校,同样的事情也适用于特许经营。当我点击“中心”创建新数据时,我想创建一个下拉列表,它还会为所有按中心分组的学校创建一个新数据

这是我的控制器

 $this->centre= $this->db->get_where('schools', array('group_by'=>'centre'))->result();

这是观看次数

       <?php foreach  ($centre as $c) { ?>
            <option value="<?php echo $c->id; ?>" <?php if(isset($school_id) && $school_id == $c->id){echo 'selected="selected"';} ?>><?php echo $c->school_name; ?></option>
        <?php }  ?>

I want to create like this, this is just an example

2 个答案:

答案 0 :(得分:0)

$this->centre 不会将数据发送到您的 view。您需要像这样将该数据传递到您的视图中:

Codeigniter 2、3

$centre= $this->db->get_where('schools', array('group_by'=>'centre'))->result();
$this->load->view('view-file', array('centre' => $centre));

Codeigniter 4

$centre= $this->db->get_where('schools', array('group_by'=>'centre'))->result();
echo view('view-file', array('centre' => $centre));

答案 1 :(得分:0)

试试这个我的朋友

    $output = '<select>';
    foreach( $center as $key => $c ) {
        $selected = (isset($school_id) && $school_id === $c->id) ? 'selected="selected"' ? '';

        if( $key === 0 ) {
          $output .= '<option value="">--Select School--</option>';
        }
        $output .= '<option value="'.$c->id.'" '.$selected .'>
            $output .= $c->school_name;
        $output .= '</option>';
    }
    $output .= '</select>';
    echo $output;

或使用 CI 表单助手

$options = array();
$selected = '';

foreach( $center as $key => $c ) {
    $options[$c->id] = $c->school_name;
    $selected = (isset($school_id) && $school_id === $c->id) ? $c->id ? '';
}

echo form_dropdown('select_tag_name', $options, $selected);
相关问题