做了几天的开发后,遇到网页文件多的问题,前端是尽可能地将增、删、改、查集中在二个页面或者三个页面中,多了就会觉得很乱。
后端的页面也尽可能地在一个文件中完成多个功能。
<?php
$OP=!empty($_POST['OP']) ? $_POST['OP'] : '';//得到操作类型
require 'DawnEisLinkConfig.php';//加载数据库
$returnArr=[];//准备返回的数组
$userName=$_SESSION['EisUser'];
$sql = "select C03,C05 from EisUser where C01='$userName'";
$res = $EisConn->query($sql);
$rows=$res->fetchAll(PDO::FETCH_ASSOC);
$userType=$rows[0]['C03'];
$restrict='';
if($userType!='管理员'){
$restrict=$rows[0]['C05'];
}
switch($OP){
case '单位':
if($restrict=''){
$sql = "select distinct C02 from EisDict where C01='单位名称'";
}else{
$sql = "select distinct C02 from EisDict where C01='单位名称' and C02='$restrict'";
}
$res = $EisConn->query($sql);
$rows=$res->fetchAll(PDO::FETCH_ASSOC);
$returnArr['data']=$rows;
break;
case '列表':
//获取具体的页数
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
//获取每页的行数
$limit = isset($_POST['limit']) ? intval($_POST['limit']) : 10;
//计算偏移值
$offset = ($page-1)*$limit;
if($restrict==''){
$sql1 = "select count(*) from computerInUserInfo";
}else{
$sql1 = "select count(*) from computerInUserInfo where C04='$restrict'";
}
$res = $EisConn->query($sql1);
$rows = $res->fetch();
$rowCount = $rows[0];
$returnArr['code']=0;
$returnArr['msg']='';
$returnArr['count']=$rowCount;
//提取要返回的值
if($restrict==''){
$sql2="SELECT TOP $limit * FROM computerInUserInfo WHERE C01 NOT IN ( SELECT TOP $offset C01 FROM computerInUserInfo ORDER BY c01 ) ORDER BY C01";
}else{
$sql2="SELECT TOP $limit * FROM computerInUserInfo WHERE C04='$restrict' and C01 NOT IN ( SELECT TOP $offset C01 FROM computerInUserInfo ORDER BY c01 ) ORDER BY C01";
}
$result=$EisConn->query($sql2);
$rows=$result->fetchAll(PDO::FETCH_ASSOC);
$returnArr['data']=$rows;
break;
case '新增':
$C01 = $_POST['C01'];
$C02 = $_POST['C02'];
$C03 = $_POST['C03'];
$C04 = $_POST['C04'];
$C05 = $_POST['C05'];
$C06 = $_POST['C06'];
$C07 = !empty($_POST['C07']) ? $_POST['C07'] : '';
$C08 = !empty($_POST['C08']) ? $_POST['C08'] : '';
$C09 = $_POST['C09'];
$C10 = $_POST['C10'];
$C11 = $_POST['C11'];
$C12 = $_POST['C12'];
$C13 = $_POST['C13'];
$C14 = $_POST['C14'];
$C15 = $_POST['C15'];
$C16 = $_POST['C16'];
$C17 = $_POST['C17'];
$sql = "insert into computerInUserInfo(C01,C02,C03,C04,C05,C06,C07,C08,C09,C10,C11,C12,C13,C14,C15,C16,C17) values('$C01','$C02','$C03','$C04','$C05','$C06','$C07','$C08','$C09','$C10','$C11','$C12','$C13','$C14','$C15','$C16','$C17')";
$result = $EisConn->exec($sql);
// $insertid=$EisConn->lastInsertId();//返回新增的主键ID
if ( $result>0 ) {
// $returnArr['data']='成功的添加了'.$result.'条记录,新增的主键ID是: '.$insertid;
$returnArr['data']='OK';
}else{
$returnArr['data']="数据记录操作错误!";
}
break;
case '修改':
$Where=$_POST['Where'];
$Set=$_POST['Set'];
$sql = "UPDATE computerInUserInfo SET $Set WHERE $Where";
$result = $EisConn->exec($sql);
if ( $result>0 ) {
$returnArr['data']='OK';
}else{
$returnArr['data']="数据记录操作错误!".$sql;
}
break;
case '删除':
$C01 = $_POST['C01'];
$sql = "delete from computerInUserInfo where C01=$C01";
$result = $EisConn->exec($sql);
if ( $result>0 ) {
$returnArr['data']='OK';
}else{
$returnArr['data']="删除操作失败!";
}
break;
default:
$returnArr['data']="不支持的操作!";
}
header('Content-type:text/json');
echo json_encode($returnArr,JSON_UNESCAPED_UNICODE);
?>
现在还没有使用面向对象的方法来写。