使用codeigniter在下拉列表中没有值

时间:2013-12-03 17:29:37

标签: php codeigniter

在下面的codeigniter代码中,我放置了控制器,模型,视图。表名为成员资格,列名为college_name。我的目标是从成员资格表中删除college_name的值并验证它请帮我做这个。我试过但下拉列表中没有值。 控制器:

function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate($select);

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            if($query->num_rows()>0){
             $status = $query->row()->account_status;}
            else {
             $status = ''; }
             //Account active
            if($status == 'active')
            {
               $this->session->set_userdata($data);
               redirect('site1/members_area');
            }
            else  if ($status == 'inactive')//Account In active
            {  $this->inactive();
              }
              else // incorrect username or password
        {
            $this->invalid();
        }
        }

    }   

型号:

function validate($select)
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
         $select = array();
        $this->db->select('college_name');
    $this->db->from('college_name');
        $query = $this->db->get('membership');
        if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $select[] = $row;
        }
    }

        return $query;

    }

视图:

<?php 
    echo form_open('login/validate_credentials');
    echo form_input('username', 'Username');
    echo form_password('password', 'Password');?>


    <select name="">
    <?php
    foreach($select as $each)
    {
        ?>
        <option value="<?=$each['select']?>"><?=$each['select']?></option>
        <?php
    }
    ?>
    </select>
    <?
        echo form_submit('submit', 'Login');
        echo anchor('login/signup', 'Create Account');
        echo form_close();
        ?>

1 个答案:

答案 0 :(得分:0)

在您的模型中,您必须返回 $ select 而不是 $ query 。 您的方法可能如下所示:

function validate()
{
    $results = $this->db
        ->select('college_name')
        ->where('username', $this->input->post('username'))
        ->where('password', md5($this->input->post('password')))
        ->from('college_name')
        ->get('membership')
        ->result();

    $select = array();
    foreach ($results as $row) 
        $select[] = $row;

    return $select;
}

你不需要在视图中循环结果,只需调用:

<?= form_dropdown('name', $select) ?>