背景:
1.数据库中有三张表,本科生数据(stu_bks),研究生数据(stu_yjs),辅导员数据(tea_fdy);
2.本科生学号(字段为XH)长度为10位,研究生学号(字段为XH)长度为9位,辅导员工号(字段为GH)长度为6位;
3.输入学号或工号,从数据库中返回相应的数据。
代码:
Controller:
1.Blog.php
<?php
class Blog extends CI_Controller{
public function index()
{
$this->load->view('finding_users');
}
}
?>
作用:加载finding_users视图。
(插播:view:finding_users)
<!DOCTYPE html>
<html>
<head>
<title>Finding User</title>
</head>
<body>
<h2>Finding Users from mysql database.</h2>
<form action="Finding" method="post">
Fill user's number:
<input type="text" name="username" size="20"> <br>
<input type="submit" name="submit" value="Find">
</form>
</body>
</html>
作用:让用户输入学号/工号,然后将数据传到Finding.php文件。
2.Finding.php:
<?php
class Finding extends CI_Controller{
public function index()
{
$usernumber= $_POST['username'];
$len = strlen($usernumber);
echo $len;
//加载模型
$this->load->model('finding_models');
//判断长度
if($len==10)
{
//调用模型的方法
$result = $this->finding_models->bks($usernumber);
}
if($len==9)
{
$result=$this->finding_models->yjs($usernumber);
}
if($len==6)
{
$result=$this->finding_models->fdy($usernumber);
}
print_r($result);
}
}
?>
model:
<?php
class Finding_models extends CI_Model{
public function bks($id)
{
$this->load->database('default');//加载数据库分组
//加载表
$this->db->from('stu_bks');
$this->db->select('*');
$this->db->where(array('XH'=>$id));
$query = $this->db->get();
return $query->row_array();
$this->db->from('tea_fdy');
}
public function yjs($id)
{
$this->load->database('default');//加载数据库分组
//加载表
$this->db->from('stu_yjs');
$this->db->select('*');
$this->db->where(array('XH'=>$id));
$query = $this->db->get();
return $query->row_array();
}
public function fdy($id)
{
$this->load->database('default');//加载数据库分组
//加载表
$this->db->from('tea_fdy');
$this->db->select('*');
$this->db->where(array('GH'=>$id));
$query = $this->db->get();
return $query->row_array();
}
}
?>