【问题标题】:Codeigniter dropdown list repeatingCodeigniter 下拉列表重复
【发布时间】:2021-09-01 14:25:04
【问题描述】:

总部下有几所学校,特许经营也是如此。当我点击“中心”创建新数据时,我想创建一个下拉列表,它也会为所有按中心分组的学校创建一个新数据

这是我的控制器

 $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

【问题讨论】:

    标签: php sql laravel codeigniter frameworks


    【解决方案1】:

    $this-&gt;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));
    

    【讨论】:

    • 我试过但没有得到我想要的结果。也许我应该更改视图文件以确保它仅显示中心。
    【解决方案2】:

    我的朋友试试这个

        $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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多