CI Model

来源:互联网 发布:怎样用c语言播放音乐 编辑:程序博客网 时间:2024/05/28 15:11

文件名需要小写,类名需要第一个字母大写 方法就是控制器所需要的数据
class User_model extends CI_Model{
    //获得所有用户
public function getAll(){
      $res=$this->db->get('user');
      return $res->result(); 
}
//在Model方法,返回给控制器
public function info(){
return '用户列表';
}
}
//控制器获得Mode中的数据
class User extends CI_Controller{
public function index(){
//加载用户模型,并且起一个别名
$this->load->model('User_model','user');
//获得模型对象调用模型中的方法
$list=$this->user->getAll();
$info=$this->user->info();
//将从模型中获得的方法通过控制器传递给视图
$this->load->view('user/index',array('list'=>$list,'info'=>$info));
}
}

//视图负责按照要求显示数据
<!DOCTYPE html>
<html>
<head>
<title>index.php/user/index</title>
</head>
<body align='center'>
<h3>显示:<?php echo $info;?></h3>
<table border="1" align='center'>
<tr><th>编号</th> <th>用户名</th> <th>密码</th> </tr>
<?php foreach($list as $items):?>
<tr>
<th><?=$items->id?></th>
<th><?=$items->username?></th>
<th><?=$items->password?></th>
</tr>
<?php endforeach;?>
</table>
</body>
</html>
//CI数据输入
$this->input->get('id');
$this->input->post('id');
$this->input->server('id');
$this->input->cookie('id');

原创粉丝点击