编辑表单选择的值输入

时间:2016-11-16 07:30:53

标签: codeigniter-3

我有选定下拉列表的问题,我单击表单edit.dropdown无法选择空(选择)如果数据库中没有数据。 我想如果数据为空,下拉这样: enter image description here 所以这里是我的代码:

//mycontroller
public function edit($id) {
        $data = array(
            'title'     => 'Users',
            'breadcrumb'=> 'User Edit',
            'groups'    => $this->groups->get_all(),
            'position'  => $this->users->get_position(),
            'report'    => $this->users->get_leader(),
            'users'     => $this->users->get_by(array('nik' => $id)),
            'content'   => 'edit'
        );
        $this->load->view ('default', $data);
    }

//my model
public function get_leader()
	{
		$idstat=array(4);
		$this->db->select('nik');
		$this->db->select('username');
		$this->db->from('t_mtr_employee');
		$this->db->where_in('t_mtr_employee.group_id',$idstat);
		$query= $this->db->get();
		return $query->result();

	}

<div class="form-group">
    <label class="control-label col-md-3">Reporting To<span
     class="required"> * </span></label>
    <div class="col-md-6">
    <select class="form-control" name="reporting">
        <option disabled>Choose</option>
        <?php 
        $id = $users->nik;
        foreach($report as $report) {
        $selected = $id == $report->nik ? 'selected' : ''; 
        echo '<option '.$selected.' value="'.$report->nik.'">'.$report->username.'</option>';
        }
        ?>
                                        
     </select> 
    </div>
</div>

上面的代码视图无法显示选择数据下拉列表的位置,我想在单击表单编辑下拉列表时选择。 我的错误代码在哪里,如何解决?

1 个答案:

答案 0 :(得分:0)

以下是我用来解决这个问题的过程......我已经离开了所有的调试 - 测试/验证,以证明这个概念是一个额外的奖励,以帮助展示如何解决这个问题......

我希望它有用

<?php
/**
 * Test code for Selection dependant upon database entries
 */

/**
 * So we want to...
 * 1. Build the Selections from the Databsae
 * 2. Set the id to choose the selection
 *
 * @var object $users
 */
// Simulate the Data from the Database for creating the Selection Dropdown
$reports_to_object = [
    (object) [ 'nik' => 1, 'username' => 'Fred' ],
    (object) [ 'nik' => 2, 'username' => 'Sam' ],
    (object) [ 'nik' => 3, 'username' => 'George' ]
];
$reports = (object) $reports_to_object;

// We expect when there is no entry from the Database
//  our id will be set to 0.

$id = 3; // Change this for testing
// Results:
// 0 = Choose - Is correctly Selected
// 1 = Fred   - Is correctly Selected
// 2 = Sam    - Is correctly Selected
// 3 = George - Is correctly Selected
var_dump($reports); // make sure our mockup data is correct
?>
<form>
    <div class="form-group">
        <label class="control-label col-md-3">Reporting To<span
                class="required"> * </span></label>
        <div class="col-md-6">
            <select class="form-control" name="reporting">
                <?php
                // DEBUG -  Remove this as we are manually setting it.
                // $id = $users->nik;
                $selected = ( $id == 0 ) ? 'selected' : '';
                echo '<option value="0" ' . $selected . '>Choose</option>';
                foreach ( $reports as $report ) {
                    $selected = ( $id == $report->nik ) ? 'selected' : '';
                    echo '<option  value="' . $report->nik . '" ' . $selected . '>' . $report->username . '</option>';
                }
                ?>
            </select>
        </div>
    </div>
</form>