1.HTML
<form class="form-inline" action="" method="get">
<a class="btn btn-info" href="{:U('add')}" value="">新增</a>
<label class="inline">名称</label>
<input type="text" name="keyword" value="{:I(keyword)}" class="form-control"
placeholder="请填写地点名称">
<button type="submit" class="btn btn-purple btn-sm">
<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>
Search
</button>
</form>
</div>
<div class="space-4"></div>
<form id="form" method="post" action="{:U('del')}">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="center"><input class="check-all" type="checkbox" value="">
</th>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<volist name="consult" id="val">
<tr>
<td class="center">
<input class="ids" type="checkbox" name="id[]"
value="{$val['id']}">
</td>
<td>{$val['id']}</td>
<td>{$val['name']}</td>
<td>{$val['six']}</td>
<td>{$val['age']}</td>
<td class="center"><a
href="{:U('edit',array('id'=>$val['id']))}">修改</a> <a
class="del" href="javascript:;"
val="{:U('del',array('id'=>$val['id']))}" title="删除">删除</a>
</td>
</tr>
</volist>
</tbody>
</table>
</form>
<div class="cf">
<input id="submit" class="btn btn-info" type="button" value="删除">
</div>
{$page}
{$page}是分页查询
2.index()函数,首页默认读取数据表的所有行,并且分页显示
public function index($p = 1)
{
$p = intval($p) > 0 ? $p : 1;
$guideModel = M('teststudent');
$pagesize = 20; #每页数量
$offset = $pagesize * ($p - 1); //计算记录偏移量
$keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
$data = array();
if ($keyword) {
$data['name'] = array('like', '%' . $keyword . '%');
}
$count = $guideModel->where($data)->count();
$img_list = $guideModel->order("id desc")->limit($offset . ',' . $pagesize)->where($data)->select();
$list = array();
foreach ($img_list as $p) {
$list[] = $p;
}
$page = new \Think\Page($count, $pagesize);
$page = $page->show();
$this->assign('consult', $list);
$this->assign('page', $page);
$this->display();
}
3.
查
点击搜索的时候,通过input的value="{:I(keyword)}"获取用户的Input里面输入的值
在index方法里面获取该值$keyword=isset($_GET['keyword'])?htmlentities($_GET['keyword']):""
然后新建模糊查询语句 $data['name']=array('like','%'.$keyword.'%')
找到这些符合查询的语句$img_list=M('test')->limit(0,5)->where($data)->select()
4.
新增
当用户点击新增按钮的时候,通过a标签中的href="{:U('add')}"跳转到PHP的add方法
add方法输出模板form,也就是把用户带到了form页面。
用户可以在form页面输入要新增的内容
当用户点击提交submit按钮的时候,from中的action="{:U('update')}"执行PHP的uptade方法
然后把用户新增的内容用M('test')->add()添加到数据表里面
public function add()
{
$this->display('form');
}
public function update($aid = 0)
{
$aid = intval($aid);
$data['id'] = isset($_POST['id']) ? $_POST['id'] : false;
$data['name'] = isset($_POST['name']) ? $_POST['name'] : false;
$data['six'] = isset($_POST['six']) ? $_POST['six'] : false;
$data['age'] = isset($_POST['age']) ? $_POST['age'] : false;
if ($aid) {
M('teststudent')->data($data)->where('id=' . $aid)->save();
addlog('编辑内容,ID:' . $aid);
$this->success('恭喜!内容编辑成功!', '/qwadmin/aaa/index');
} else {
$aid = M('teststudent')->data($data)->add();
addlog('编辑内容,ID:' . $aid);
$this->success('恭喜!内容新增成功!', '/qwadmin/aaa/index');
}
}
5.
修改
但用户点击修改按钮的时候,a标签的href="{:U('edit'),array('id'=>$val['id'])}"执行PHP的edit方法,并把id传过去
在edit方法中验证一下有没有该Id的数据行,
有就通过value="{$item['name']}"把值给form网页的输入框,并通过$this->display('form')跳转到form页面
在form页面点击提交的时候,rom中的action="{:U('update')}"执行PHP的uptade方法
uptade方法通过$_POST['name']看在首页是否传入了值
然后通过M('test')->where($data)->save()保存修改后的值
public function update($aid = 0)
{
$aid = intval($aid);
$data['id'] = isset($_POST['id']) ? $_POST['id'] : false;
$data['name'] = isset($_POST['name']) ? $_POST['name'] : false;
$data['six'] = isset($_POST['six']) ? $_POST['six'] : false;
$data['age'] = isset($_POST['age']) ? $_POST['age'] : false;
if ($aid) {
M('teststudent')->data($data)->where('id=' . $aid)->save();
addlog('编辑内容,ID:' . $aid);
$this->success('恭喜!内容编辑成功!', '/qwadmin/aaa/index');
} else {
$aid = M('teststudent')->data($data)->add();
addlog('编辑内容,ID:' . $aid);
$this->success('恭喜!内容新增成功!', '/qwadmin/aaa/index');
}
}
public function edit($id = 0)
{
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
$aid = intval($id);
$guide_typeModel = M('teststudent');
$item = $guide_typeModel->where("id='$id'")->find();
if (!$item) {
$this->error('参数错误!');
}
$this->assign('item', $item);
$this->display('form');
}
6.
删除(删除链接)
当用户点击删除链接的时,a标签的href="{:U('del'),array('id'=>$val['id'])}"执行PHP的del方法,并把id传过去
在del方法中通过M('test')->where($data)->delete()删除该数据行
public function del()
{
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
if ($id) {
if (is_array($id)) {
$ids = implode(',', $id);
$map['id'] = array('in', $id);
} else {
$map = 'id=' . $id;
}
$data = array();
$data['is_delete'] = '1';
$result = M('teststudent')->where($map)->data($data)->delete();
if ($result !== false) {
addlog('删除内容,AID:' . $id);
$this->success('恭喜,内容删除成功!');
} else {
$this->error('抱歉,未知错误!');
}
} else {
$this->error('参数错误!');
}
}
7.
删除(按钮submit)
当点击删除按钮sumbit时,通过form 的action="{:U('del')}"执行PHP的del方法
用$_REQUEST['id']获取form的method='post' 的post或者get的id
通过$map['id']=array('in',$id);验证该id
然后通过M('test')->where($map)->delete()删除这些数据行
原PHP文件https://download.youkuaiyun.com/download/csdn_zym1101/13011714
原HTML文件https://download.youkuaiyun.com/download/csdn_zym1101/13011870