在下面的codeigniter代码中,我已经放置了控制器,模型和视图。在我的登录中,我已经在下拉列表中创建用户名,密码和大学以验证用户。但是我在college_name下载错误消息:未定义变量:验证,消息:为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();
?>
答案 0 :(得分:1)
尝试在这里使用正确的驼峰案例
$this->Membership_model->validate();
为$this->membership_model->validate();
您还需要将$ data变量传递给查看文件。
$this->load->view('login_form',$data);
修改强>
在validate()方法中,您对2个不同的查询使用了相同的变量($ query)并返回查询结果。确保你在那里返回了正确的结果。