PHP ? EasyUI DataGrid 资料存的方式介绍


Posted in PHP onNovember 07, 2012

继上篇文章 PHP ? EasyUI DataGrid 资料取的方式,本篇继续讲述,如何操作 DataGrid,把资料存入资料库,并实现 MVC 架构,将资料层分离、独立运作。
本篇文章主要是改良,原 EasyUI DataGrid 的范例  Build CRUD Application with jQuery EasyUI。

在官方范例中已经示范如何操作资料,但其中有个问题就是,你要操作资料的每个动作都需要一支对应的程式才能动作,像是新增、删除、修改以及取得资料,总共至少要有四支对应程式才能运作。

读者可以想想,这还只是一支单档 使用者的基本资料维护而已,一般系统光基本资料都有十几支甚至几十支程式在运作,所以这样的方式,势必要改良才能运作在实务上。
在来按造 多层次架构设计前言 的精神,大家可以发现这四支程式其实对每一个基本资料的操作来说,都是大同小异的,所以是可以把他标准化,用成一个固定框架,供后面类似程式来使用。

这部分,会分几篇文章来逐渐完成这各过程,藉由这逐渐演进的过程,来了解框架是如何成形的。
首先本篇,先来介绍,如何把分散的四支程式集中成为一支程式来呼叫,在读者往下阅读之前,可先在了解 PHP ? EasyUI DataGrid 资料取的方式 以及官方范例   Build CRUD Application with jQuery EasyUI 的运作方式,至少要能把范例 Run 起来,run 这个动作是很重要的,不要光看而已,亲身去测试才能了解其中的问题点。

要能实现将四支程式改成一支程式来运作,其实关键很简单,就是去改每个操作动作时呼叫的 url,改成都呼叫 DAL 端的程式 dal_user.php,接下来在呼叫前,都要传递一个 type 参数告诉 dal 你要进行何种动作。
目前 type 定义了下面四个动作
add 新增
mod 修改
del 删除
data 取得资料
了解 想要 dal 作哪些动作后,就可以开始来撰写 dal 程式了,当然现在这各 dal 还是一个非标准化的程式,但是他已经做到 MVC 的精神,把资料存取层跟表现层 分离开了,后面的文章, 会再来介绍,如何把本篇介绍的程式来标准化 dal 以及 UI 表现层。

dal_user.php

<?php 
$result = false; if (!empty($_REQUEST['type']) ) 
{ 
require_once(".\..\db\DB_config.php"); 
require_once(".\..\db\DB_class.php"); 
$db = new DB(); 
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']); 
$tablename = "STUser"; 
$type = $_REQUEST['type']; 
if($type == "del") 
{ 
$id = $_REQUEST['id']; 
$sql = "delete from STUser where UNum=$id"; 
$result = $db->query($sql); 
}else if($type == "data"){ 
$page = isset($_POST['page']) ? intval($_POST['page']) : 1; 
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
$offset = ($page-1)*$rows; 
$result = array(); 
$db->query("select count(*) As Total from $tablename"); 
$row = $db->fetch_assoc(); 
$result["total"] = $row["Total"]; 
$db->query("select * from $tablename limit $offset,$rows"); 
$items = array(); 
while($row = $db->fetch_assoc()){ 
array_push($items, $row); 
} 
$result["rows"] = $items; 
echo json_encode($result); 
}else{ 
$STUID = $_REQUEST['STUID']; 
$Password = $_REQUEST['Password']; 
$Nickname = $_REQUEST['Nickname']; 
$Birthday = $_REQUEST['Birthday']; 
if (!empty($_REQUEST['id']) ) { 
$id = $_REQUEST['id']; 
$sql = "update $tablename set STUID='$STUID',Password='$Password',Nickname='$Nickname' where UNum=$id"; 
}else{ // is add 
$sql = "insert into $tablename (STUID, Password, Nickname, DBSTS) values('$STUID','$Password','$Nickname', 'A')"; 
} 
$result = $db->query($sql); 
} 
} 
if($type != "data") 
{ 
if ($result == "true"){ 
echo json_encode(array('success'=>true)); 
} else { 
echo json_encode(array('msg'=>'had errors occured. ' . $result)); 
} 
} 
?>

dal 资料存取层 定义完了以后,就可以来实现 UI 介面来呼叫 dal,因为是使用 AJAX 的方式 来存取资料,所以 MVC 中的控制层有一部分是放在 介面层中,这部分,后面可以在用 JavaScript 将这部分的控制层标准化,在藉由 php 后端来传递参数呼叫,如此一来,则还是将所有控制大权集中在一支程式中,这些后面文章会再来介绍,这边先暂时打住。

datagrid.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>easyUI datagrid</title> 
<link rel="stylesheet" type="text/css" href="./../JS/EasyUI/themes/default/easyui.css"> 
<link rel="stylesheet" type="text/css" href="./../JS/EasyUI/themes/icon.css"> 
<script type="text/javascript" src="./../JS/jquery.js"></script> 
<script type="text/javascript" src="./../JS/EasyUI/jquery.easyui.min.js"></script> 
<script type="text/javascript" src="./../JS/EasyUI/easyui-lang-zh_CN.js"></script> 
<style type="text/css"> 
#fm{ 
margin:0; 
padding:10px 30px; 
} 
.ftitle{ 
font-size:14px; 
font-weight:bold; 
color:#666; 
padding:5px 0; 
margin-bottom:10px; 
border-bottom:1px solid #ccc; 
} 
.fitem{ 
margin-bottom:5px; 
} 
.fitem label{ 
display:inline-block; 
width:80px; 
} 
</style> 
<script type="text/javascript"> 
var url; 
function newUser(){ 
$('#dlg').dialog('open').dialog('setTitle','New User'); 
$('#fm').form('clear'); 
url = 'dal_user.php?type=add'; 
} 
function editUser(){ 
var row = $('#myDG').datagrid('getSelected'); 
if (row){ 
if(typeof(row.UNum) !== 'undefined') 
{ 
$('#dlg').dialog('open').dialog('setTitle','Edit User'); 
$('#fm').form('load',row); 
url = 'dal_user.php?type=mod&id='+row.UNum; 
}else{ 
alert("undefined"); 
} 
} 
} 
function saveUser(){ 
$('#fm').form('submit',{ 
url: url, 
onSubmit: function(){ 
//alert('sub :'+ url); 
return $(this).form('validate'); 
}, 
success: function(result){ 
var result = eval('('+result+')'); 
//alert(result.success); 
if (result.success){ 
$('#dlg').dialog('close'); // close the dialog 
$('#myDG').datagrid('reload'); // reload the user data 
} else { 
$.messager.show({ 
title: 'Error', 
msg: result.msg 
}); 
} 
} 
}); 
} 
function removeUser(){ 
var row = $('#myDG').datagrid('getSelected'); 
if (row){ 
$.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){ 
if (r){ 
//alert(row.UNum); 
$.post('dal_user.php', {type:'del', id:row.UNum}, function(result){ 
if (result.success){ 
$('#myDG').datagrid('reload'); // reload the user data 
} else { 
$.messager.show({ // show error message 
title: 'Error', 
msg: result.msg 
}); 
} 
},'json'); 
} 
}); 
} 
} 
</script> 
</head> 
<body> 
<h2>easyUI datagrid url 存取?y?</h2> 
<table id="myDG" class="easyui-datagrid" style="width:700px;height:450px" 
url="dal_user.php?type=data" toolbar="#toolbar" 
title="Load Data" iconCls="icon-save" pagination="true" 
toolbar="#toolbar" rownumbers="true" fitColumns="true" singleSelect="true"> 
<thead> 
<tr> 
<th field="STUID" width="120">User ID</th> 
<th field="Password" width="80" align="right">Password</th> 
<th field="Birthday" width="80" align="right">Birthday</th> 
<th field="Nickname" width="200">Nickname</th> 
<th field="DBSTS" width="60" align="center">DBSTS</th> 
</tr> 
</thead> 
</table> 
<div id="toolbar"> 
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a> 
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> 
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">Remove User</a> 
</div> 
<div id="dlg" class="easyui-dialog" style="width:400px;height:350px;padding:10px 20px" 
closed="true" buttons="#dlg-buttons"> 
<div class="ftitle">User Information</div> 
<form id="fm" method="post" novalidate> 
<div class="fitem"> 
<label>User ID:</label> 
<input name="STUID" class="easyui-validatebox" required="true"> 
</div> 
<div class="fitem"> 
<label>Password:</label> 
<input name="Password" class="easyui-validatebox" required="true"> 
</div> 
<div class="fitem"> 
<label>Nickname:</label> 
<input name="Nickname"> 
</div> 
<div class="fitem"> 
<label>Birthday:</label> 
<input name="Birthday" class="easyui-validatebox" validType="email"> 
</div> 
</form> 
</div> 
<div id="dlg-buttons"> 
<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a> 
<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a> 
</div> 
</body> 
</html>

运作结果画面如下所示:

PHP ? EasyUI DataGrid 资料存的方式介绍

PHP ? EasyUI DataGrid 资料存的方式介绍

PHP 相关文章推荐
PHP的面试题集
Nov 19 PHP
用C/C++扩展你的PHP 为你的php增加功能
Sep 06 PHP
PHP HTML JavaScript MySQL代码如何互相传值的方法分享
Sep 30 PHP
解析php框架codeigniter中如何使用框架的session
Jun 24 PHP
php字符串截取的简单方法
Jul 04 PHP
浅析PHP原理之变量(Variables inside PHP)
Aug 09 PHP
php字符串函数学习之substr()
Mar 27 PHP
PHP防盗链的基本思想 防盗链的设置方法
Sep 25 PHP
在WordPress中使用PHP脚本来判断访客来自什么国家
Dec 10 PHP
PHP快速生成各种信息提示框的方法
Feb 03 PHP
PHP的swoole扩展安装方法详细教程
May 18 PHP
解决Laravel 使用insert插入数据,字段created_at为0000的问题
Oct 11 PHP
PHP ? EasyUI DataGrid 资料取的方式介绍
Nov 07 #PHP
PHP正确解析UTF-8字符串技巧应用
Nov 07 #PHP
nginx+php-fpm配置文件的组织结构介绍
Nov 07 #PHP
使用 PHPMAILER 发送邮件实例应用
Nov 07 #PHP
PHP数据集构建JSON格式及新数组的方法
Nov 07 #PHP
php动态实现表格跨行跨列实现代码
Nov 06 #PHP
对象失去焦点时自己动提交数据的实现代码
Nov 06 #PHP
You might like
php利用header函数实现文件下载时直接提示保存
2009/11/12 PHP
php进行支付宝开发中return_url和notify_url的区别分析
2014/12/22 PHP
php的GD库imagettftext函数解决中文乱码问题
2015/01/24 PHP
PHP实现批量重命名某个文件夹下所有文件的方法
2017/09/04 PHP
PHP观察者模式定义与用法实例分析
2019/03/22 PHP
基于jquery自定义图片热区效果
2012/07/21 Javascript
setTimeout()递归调用不加引号出错的解决方法
2014/09/05 Javascript
JS实现双击编辑可修改状态的方法
2015/08/14 Javascript
基于Jquery实现万圣节快乐特效
2015/11/01 Javascript
js验证框架实现代码分享
2016/05/18 Javascript
EasyUI的doCellTip实现鼠标放到单元格上提示单元格内容
2016/08/24 Javascript
基于jQuery实现发送短信验证码后的倒计时功能(无视页面关闭)
2016/09/02 Javascript
轻松理解Javascript变量的相关问题
2017/01/20 Javascript
详解jquery选择器的原理
2017/08/01 jQuery
原生nodejs使用websocket代码分享
2018/04/07 NodeJs
解决vue中监听input只能输入数字及英文或者其他情况的问题
2018/08/30 Javascript
浅谈vuex的基本用法和mapaction传值问题
2019/11/08 Javascript
uni-app使用countdown插件实现倒计时
2020/11/01 Javascript
pthon贪吃蛇游戏详细代码
2019/01/27 Python
django 微信网页授权登陆的实现
2019/07/30 Python
python中sys模块是做什么用的
2020/08/16 Python
python -v 报错问题的解决方法
2020/09/15 Python
一款恶搞头像特效的制作过程 利用css3和jquery
2014/11/21 HTML / CSS
美国睫毛、眉毛精华液领导品牌:RevitaLash Cosmetics
2018/03/26 全球购物
Under Armour瑞典官方网站:美国高端运动科技品牌
2018/11/21 全球购物
毕业生个人求职信范文分享
2014/01/05 职场文书
优秀士兵先进事迹
2014/02/06 职场文书
结婚喜宴主持词
2014/03/14 职场文书
销售求职信范文
2014/05/26 职场文书
抵押贷款承诺书
2014/05/30 职场文书
2014小学生国庆65周年演讲稿
2014/09/21 职场文书
2016新年感言
2015/08/03 职场文书
公文格式,规则明细(新手收藏)
2019/07/23 职场文书
python实现自定义日志的具体方法
2021/05/28 Python
windows11怎么查看wifi密码? win11查看wifi密码的技巧
2021/11/21 数码科技
速龙x4-860k处理器相当于i几
2022/04/20 数码科技