一个ci框架的小demo
最近在学习ci框架,作为一个初学者,在啃完一遍官方文档并也跟着官方文档的例程(新闻发布系统)做了一遍,决定在将之前练习PHP与MySQL数据库的用户管理系统再用ci框架实现一遍。
主要由三个简单的界面构成:
看起来很简单对不对!没有关系,这不重要~
接下来我们来看代码:
ci的配置我就不用讲了哈,我的配置如下:
database.php 配置文件
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
一定要配置config.php中的base_url,原因你会知道的
$config['base_url'] = 'http://localhost:81/ciDemo/Code/';
接下来是控制器的代码:
我建立了一个user.php的控制器,目录是application/controllers/user.php
<?php
/**
* 数据库的增删改查
*/
class User extends CI_controller
{
function __construct()
{
parent::__construct();
$this->load->model('user_model');
// $this->load->helper('url_helper');
$this->load->helper('url');
}
public function index()
{
// $this->load->model('user_model');
$arr = $this->user_model->getAll();
// var_dump($arr);
$data['title'] = 'Userlist';
$this->load->view('templates/header', $data);
$this->load->view('user/index',array('arr'=>$arr));
$this->load->view('templates/footer');
}
public function insert()
{
$this->load->helper('form');
$data['title'] = 'Create a new user';
// $this->load->model('user_model');
// $arr = array('uaername' => '111','password' => 'md5(222)' );
$this->load->view('templates/header', $data);
$this->load->view('user/adduser');
$this->load->view('templates/footer');
}
public function insert_ok()
{
// echo "join";
// if ($this->input->post('submit_user')) {
$arr = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$query = $this->user_model->user_insert($arr);
// var_dump($query);
if ($query) {
// echo "添加成功";
// $this->index();
redirect('user/index');
} else {
echo "添加失败";
}
// }
}
public function update()
{
$this->load->helper('form');
$data['title'] = 'Update user';
// $this->load->model('user_model');
// $arr = array('uaername' => '111','password' => 'md5(222)' );
$id = $this->uri->segment(3, 0);
// echo $id;
$arr = $this->user_model->user_select($id);
// var_dump($arr);
//怎么把指定ID的username传到视图中去
$this->load->view('templates/header', $data);
$this->load->view('user/editUser', array('get_username'=>$arr));
$this->load->view('templates/footer');
}
public function update_ok()
{
// $this->load->model('user_model');
// $arr = array('username' => 'kkk','password' => 'md5(333)' );
$arr = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
// $id = $_GET['id'];
//怎么获取ID值?
$id = $this->uri->segment(3, 0);
$query = $this->user_model->user_update($id,$arr);
if ($query) {
// echo "删除成功";
// $this->index();
redirect('user/index');
} else {
echo "更新失败";
}
}
public function delete()
{
$id = $this->uri->segment(3, 0);
// $this->load->model('user_model');
$query = $this->user_model->user_delete($id);
if ($query) {
// echo "删除成功";
// $this->index();
redirect('user/index');
} else {
echo "删除失败";
}
}
// public function select()
// {
// // $this->load->model('user_model');
// $arr = $this->user_model->user_select();
// print_r($arr);
// echo $arr[0]->id;
// }
}
模型的代码:
目录是application/models/user_model.php
<?php
/**
* 数据库增删改查......
*/
class User_model extends CI_Model
{
//构造函数
function __construct()
{
parent::__construct();
//connect to the database
$this->load->database();
//$this->load->insert($t_name,$data)
}
//所有数据
function getAll()
{
$res = $this->db->get('user');
return $res->result_array();
}
//增加数据
function user_insert($arr)
{
return $this->db->insert('user', $arr);
}
//更新数据
function user_update($id,$arr)
{
$this->db->where('id',$id);
return $this->db->update('user',$arr);
}
//删除数据
function user_delete($id)
{
$this->db->where('id',$id);
return $this->db->delete('user');
}
//查找数据
function user_select($id)
{
$this->db->where('id',$id);
$this->db->select('*');
$query = $this->db->get('user');
return $query->result_array();
}
}
接下来是视图:
用户列表的视图 application/views/user/index.php
<?php
// var_dump($arr);
?>
<h2>用户列表<a href="<?php echo site_url('user/insert');?>">添加用户</a></h2>
<table border="1" cellspacing="0" cellpadding="0" width="80%" bgcolor="#ABCDEF">
<tr>
<td>编号</td>
<td>用户名</td>
<td>操作</td>
</tr>
<?php $i=1; foreach($arr as $row):?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $row['username'];?></td>
<td><a href="<?php echo site_url().'/user/update/'.$row['id'];?>">更新</a>|<a href="<?php echo site_url().'/user/delete/'.$row['id'];?>">删除</a></td>
</tr>
<?php $i++; endforeach;?>
</table>
添加用户的视图:application/views/user/adduser.php
<!-- <h2>添加用户</h2> -->
<form action="<?php echo site_url("user/insert_ok");?>" method="post">
<?php //echo form_open('user/insert')?>
<table border="1" cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="80%">
<tr>
<td>用户名</td>
<td><input type="text" name="username" id="username" placeholder="请输入合法用户名" required></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" id="password" placeholder="请输入密码" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加用户" name="submit_user"></td>
</tr>
</table>
</form>
更新用户的视图:application/views/user/edituser.php
<!-- <h2>更新用户</h2> -->
<form action="<?php foreach ($get_username as $row):?><?php echo site_url().'/user/update_ok/'.$row['id'];?><?php endforeach;?>" method="post">
<table border="1" cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="80%">
<tr>
<td>用户名</td>
<td><input type="text" name="username" id="" placeholder="请输入合法用户名" required value="<?php foreach ($get_username as $row):?><?php echo $row['username'];?><?php endforeach;?>"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" id="" placeholder="请输入密码" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="更新用户"></td>
</tr>
</table>
</form>
为什么我的视图都没有HTML头和尾,因为我把他们分开写在了views视图下的templates文件夹里:
application/views/templates/header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
application/views/templates/footer.php
<em>© 2016</em>
</body>
</html>
控制器,视图,模型的代码我都贴完了,希望对你有帮助!
第一次写CSDN,有点没有耐心~