闲暇之余,研究了一下CI
框架的使用,一个说简单不简单,说复杂又不复杂的尴尬框架。由于之前一直接触和使用TP
框架和YII2
框架,对于CI
的上手,还是折腾了半天,官方文档真的是相当简洁啊,简言之,算是有点小眉目,遂写下,已记之。常言道:好记性不如烂笔头!(咳、咳、对于TP
和YII
为啥没写,我认为,主要原因是因为懒)
1 | CREATE TABLE `user` ( |
打开你models
所在的目录,新建一个文件User_model.php
。
打开你controllers
所在的目录,新建一个文件User.php
。
打开你view
所在的目录,新建文件index.php
,edit.php
,create.php
。
OK,准备工作已经就绪,开始撸代码了,哇卡卡,有点激动啊n(≧▽≦)n
查询数据:
在
model
中的User_model.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/**
* 查询学生数据
* @param boolean $slug [description]
* @return [type] [description]
*/
public function get_user($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('user');
return $query->result_array();
}
$query = $this->db->get_where('user', $slug);
return $query->row_array();
}在
controllers
中的User.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/**
* [__construct description]
*/
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
$this->load->helper('url_helper');
}
/**
* [index description]
* @return [type] [description]
*/
public function index()
{
$data['user'] = $this->user_model->get_user();
$this->load->view('user/index', $data);
}在
view
中的index.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29<html>
<head>
<meta charset="utf-8"/>
<title>用户详情</title>
</head>
<body>
<h3><a href="<?php echo site_url('user/create'); ?>">新增用户</a></h3>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</tr>
foreach ($user as $key => $value):
<tr>
<td>echo $value['id']; </td>
<td>echo $value['name']; </td>
if ($value['sex'] == 1):
<td>男</td>
else:
<td>女</td>
endif
<td><a href="<?php echo site_url('user/edit/'.$value['id']); ?>">编辑</a>/<a href="<?php echo site_url('user/delete/'.$value['id']); ?>">删除</a></td>
</tr>
endforeach
</table>
</body>
</html>
修改数据:
在
model
中的User_model.php
写入下面的代码:(由于减少代码量,将增加和修改合并为一个方法,也算一种代码优化)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/**
* 设置学生数据
* @param boolean $data 要修改的数据
* @param boolean $slug where条件
*/
public function set_user($data = FALSE, $slug = FALSE)
{
if ($slug === FALSE)
{
return $this->db->insert('user', $data);
}
else
{
return $this->db->update('user', $data, $slug);
}
}在
controllers
中的User.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24/**
* [edit description]
* @return [type] [description]
*/
public function edit()
{
$this->load->helper('url');
$id = $this->uri->segment(3, 0);
$slug = array('id' => $id);
if ($_GET)
{
$data['name'] = $_GET['name'];
$data['sex'] = $_GET['sex'];
if ($this->user_model->set_user($data, $slug))
{
redirect('user/index');
}
}
else
{
$data['user'] = $this->user_model->get_user($slug);
$this->load->view('user/edit', $data);
}
}在
view
中的edit.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29<html>
<head>
<meta charset="utf-8"/>
<title>编辑信息</title>
</head>
<body>
<form action="">
<table border="1">
<br/>
<tr>
<label for="name">姓名:</label>
<input type="text" name="name" value="<?php echo $user['name'] ;?>">
</tr>
<br/>
<br/>
<tr>
<label for="sex">性别:</label>
<input type="radio" value="1" name="sex" if ($user['sex'] == 1): checked endif />男
<input type="radio" value="2" name="sex" if ($user['sex'] == 2): checked endif />女
</tr>
<br/>
<br/>
<tr>
<input type="submit" value="提交">
</tr>
</table>
</form>
</body>
</html>
新增数据:
在
model
中的User_model.php
写入下面的代码:(由于减少代码量,将增加和修改合并为一个方法,也算一种代码优化)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/**
* 设置学生数据
* @param boolean $data 要修改的数据
* @param boolean $slug where条件
*/
public function set_user($data = FALSE, $slug = FALSE)
{
if ($slug === FALSE)
{
return $this->db->insert('user', $data);
}
else
{
return $this->db->update('user', $data, $slug);
}
}在
controllers
中的User.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* [create description]
* @return [type] [description]
*/
public function create()
{
if ($_GET)
{
$data['name'] = $_GET['name'];
$data['sex'] = $_GET['sex'];
if ($this->user_model->set_user($data))
{
redirect('user/index');
}
}
else
{
$this->load->view('user/create');
}
}在
view
中的create.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29<html>
<head>
<meta charset="utf-8"/>
<title>新增信息</title>
</head>
<body>
<form action="">
<table border="1">
<br/>
<tr>
<label for="name">姓名:</label>
<input type="text" name="name" value="">
</tr>
<br/>
<br/>
<tr>
<label for="sex">性别:</label>
<input type="radio" value="1" name="sex" checked />男
<input type="radio" value="2" name="sex" />女
</tr>
<br/>
<br/>
<tr>
<input type="submit" value="提交">
</tr>
</table>
</form>
</body>
</html>
删除数据:
在
model
中的User_model.php
写入下面的代码:1
2
3
4
5
6
7
8
9/**
* 删除学生数据
* @param boolean $where where条件
* @return [type] [description]
*/
public function del_user($where = FALSE)
{
return $this->db->delete('user', $where);
}
在
controllers
中的User.php
写入下面的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/**
* [delete description]
* @return [type] [description]
*/
public function delete()
{
$id = $this->uri->segment(3, 0);
$where = array('id' => $id);
if ($this->user_model->del_user($where))
{
redirect('user/index');
}
else
{
echo "<script>alert('删除失败');window.location.href='".site_url('user/index')."'</script>";
}
}
关于CI
框架的增删改查功能,就写完了,是不是很简单。这个博客草稿N天前就有了,到现在正文才竣工,我觉得真实原因在于我懒,心宽体胖的我已经突破了人生中体重最高值。也通过这个博客,写下减肥的决心。争取早日减到110斤,把自己嫁出去……哇咔咔O(∩_∩)O哈哈哈~