CI框架的curd(增、删、改、查)操作

来源:互联网 发布:flashfxp连接linux 编辑:程序博客网 时间:2024/05/21 09:04

本文还涉及模型与视图页面只适合借鉴,不适合copy

// A . 完成信息的展示(查找)

public function getUserData()

{
// b. 实例化模型的时候也支持简写,传递第二个参数
$this->load->model('user_model', 'user');


// 调用模型的方法,使用实例化时候的简写
$userData = $this->user->getUserData(10);

// 载入视图
$this->load->view('user/getUserData', array('userData'=> $userData));

}


// B. 完成信息的添加(添加)
public function addUser()
{
// 如果需要使用site_url函数和redirect函数,还需要手工的载入函数的文件
// $this->load->helper('url');

if($_SERVER['REQUEST_METHOD'] == "POST"){
// 收集表单
$data = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'email' => $this->input->post('email'),
);
// 实例化模型,完成入库
$this->load->model('user_model', 'user');
$result = $this->user->addUser($data);
if($result === true){
// 插入成功,使用下面的函数进行跳转
// 参数(控制器/方法)
redirect('user/getUserData');
}else{
redirect('user/addUser');
}
}
$this->load->view('user/addUser');
}




// C. 完成会员删除(删除)
public function delUser($id = 'id', $number)
{
// 1. 接受ID
$id = $this->uri->segment(4);
// 2. 实例化模型 $this->load 装载器 
// $this 超全局对象:控制器和模型和视图里面完全一样

$this->load->model('user_model', 'user');
$where = array('id' => $id);
$result = $this->user->delUser($where);
if($result === true){
redirect('user/getUserData');
}else{
redirect('user/getUserData');
}
}


// D. 完成更新(修改)
public function edtUser()
{
// 2. 实例化模型 user_model
$this->load->model('user_model', 'user');

if($_SERVER['REQUEST_METHOD'] == "POST"){
// 接受表单数据
$data = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
);
// 特别注意,主键id是放在表单的隐藏域里面的
$where = array('id' => $this->input->post('id'));
// 实例化模型 更新 : 1. 数据 2. 条件
$result = $this->user->edtUser($data, $where);
if($result === true){
redirect('user/getUserData');
}else{
redirect('user/getUserData');
}
}
// 1. 接受
$id = $this->uri->segment(4);
$where = array('id' => $id);
$userInfo = $this->user->findOne($where);
$this->load->view('user/edtUser', array('userInfo' => $userInfo));
}
1 0