在下面的代码点火器代码中,我放置了控制器、模型和视图。在我的登录中,我在下拉列表中创建了用户名、密码和大学,以验证用户。但我在collecte_name下拉列表中出错消息:未定义的变量:validate,消息:为foreach()提供的参数无效。请帮助我解决问题。控制器
function college()
{
$this->load->helper('url');
$data = array();
$this->load->model("membership_model");
$data['validate'] = $this->Membership_model->validate();
$this->load->view('login_form');
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
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()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$this->db->where('college_name', $this->input->post('college_name'));
$query = $this->db->query("SELECT college_name FROM membership ");
$query = $this->db->get('membership');
return $query->result();
}
视图:
<?php
echo form_open('login/validate_credentials');
echo form_input('username', 'Username');
echo form_password('password', 'Password');
$array = array();
foreach($validate as $row ){
$array[] = $row->college_name;
}
echo form_dropdown('validate', $array);
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Create Account');
echo form_close();
?>
尝试在此处使用正确的驼色案例
$this->Membership_model->validate();
作为$this->membership_model->validate();
您还需要将$data变量传递给视图文件。
$this->load->view('login_form',$data);
编辑:
在validate()方法中,您对两个不同的查询使用了相同的变量($query),并返回查询结果。确保您在那里返回了正确的结果。