欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP – EasyUI DataGrid 資料存的方式介紹

 更新時(shí)間:2012年11月07日 14:13:01   作者:  
繼上篇文章 PHP – EasyUI DataGrid 資料取的方式,本篇一條小龍繼續(xù)講述,如何操作 DataGrid,把資料存入資料庫(kù),并實(shí)現(xiàn) MVC 架構(gòu),將資料層分離、獨(dú)立運(yùn)作

繼上篇文章 PHP – EasyUI DataGrid 資料取的方式,本篇繼續(xù)講述,如何操作 DataGrid,把資料存入資料庫(kù),并實(shí)現(xiàn) MVC 架構(gòu),將資料層分離、獨(dú)立運(yùn)作。
本篇文章主要是改良,原 EasyUI DataGrid 的范例  Build CRUD Application with jQuery EasyUI。

在官方范例中已經(jīng)示范如何操作資料,但其中有個(gè)問(wèn)題就是,你要操作資料的每個(gè)動(dòng)作都需要一支對(duì)應(yīng)的程式才能動(dòng)作,像是新增、刪除、修改以及取得資料,總共至少要有四支對(duì)應(yīng)程式才能運(yùn)作。

讀者可以想想,這還只是一支單檔 使用者的基本資料維護(hù)而已,一般系統(tǒng)光基本資料都有十幾支甚至幾十支程式在運(yùn)作,所以這樣的方式,勢(shì)必要改良才能運(yùn)作在實(shí)務(wù)上。
在來(lái)按造 多層次架構(gòu)設(shè)計(jì)前言 的精神,大家可以發(fā)現(xiàn)這四支程式其實(shí)對(duì)每一個(gè)基本資料的操作來(lái)說(shuō),都是大同小異的,所以是可以把他標(biāo)準(zhǔn)化,用成一個(gè)固定框架,供后面類似程式來(lái)使用。

這部分,會(huì)分幾篇文章來(lái)逐漸完成這各過(guò)程,藉由這逐漸演進(jìn)的過(guò)程,來(lái)了解框架是如何成形的。
首先本篇,先來(lái)介紹,如何把分散的四支程式集中成為一支程式來(lái)呼叫,在讀者往下閱讀之前,可先在了解 PHP – EasyUI DataGrid 資料取的方式 以及官方范例   Build CRUD Application with jQuery EasyUI 的運(yùn)作方式,至少要能把范例 Run 起來(lái),run 這個(gè)動(dòng)作是很重要的,不要光看而已,親身去測(cè)試才能了解其中的問(wèn)題點(diǎn)。

要能實(shí)現(xiàn)將四支程式改成一支程式來(lái)運(yùn)作,其實(shí)關(guān)鍵很簡(jiǎn)單,就是去改每個(gè)操作動(dòng)作時(shí)呼叫的 url,改成都呼叫 DAL 端的程式 dal_user.php,接下來(lái)在呼叫前,都要傳遞一個(gè) type 參數(shù)告訴 dal 你要進(jìn)行何種動(dòng)作。
目前 type 定義了下面四個(gè)動(dòng)作
add 新增
mod 修改
del 刪除
data 取得資料
了解 想要 dal 作哪些動(dòng)作后,就可以開(kāi)始來(lái)撰寫(xiě) dal 程式了,當(dāng)然現(xiàn)在這各 dal 還是一個(gè)非標(biāo)準(zhǔn)化的程式,但是他已經(jīng)做到 MVC 的精神,把資料存取層跟表現(xiàn)層 分離開(kāi)了,后面的文章, 會(huì)再來(lái)介紹,如何把本篇介紹的程式來(lái)標(biāo)準(zhǔn)化 dal 以及 UI 表現(xiàn)層。

dal_user.php

復(fù)制代碼 代碼如下:

<?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 資料存取層 定義完了以后,就可以來(lái)實(shí)現(xiàn) UI 介面來(lái)呼叫 dal,因?yàn)槭鞘褂?AJAX 的方式 來(lái)存取資料,所以 MVC 中的控制層有一部分是放在 介面層中,這部分,后面可以在用 JavaScript 將這部分的控制層標(biāo)準(zhǔn)化,在藉由 php 后端來(lái)傳遞參數(shù)呼叫,如此一來(lái),則還是將所有控制大權(quán)集中在一支程式中,這些后面文章會(huì)再來(lái)介紹,這邊先暫時(shí)打住。

datagrid.php
復(fù)制代碼 代碼如下:

<!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 存取測(cè)試</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>

運(yùn)作結(jié)果畫(huà)面如下所示:

相關(guān)文章

最新評(píng)論