题外话:
本来这个项目确实是打算使用java web的框架的,但是后来觉得这个项目实在是比较小,没有必要使用框架,于是就用php写了。改成框架结构也可以,不是太难。还有就是现在不要取下载我的那个资源,资源我本来意思是免费下载的,但是没有发现更改积分价格的地方,那就等我全部做完了把整个项目都上传上去在下载吧,不然真的不划算。
1 对voterecord 的操作
1.1 voterecord表的作用与结构
voterecord 表记录的是调查记录
RecordID 主键 自增长 会被其他表引用,例如question,answer表
starttime endtime 起始时间 结束实际那
status 状态 (已停止、进行中、未启用)
describtion 调查描述:原因
welcome end 欢迎界面弹出信息 问卷调查结束弹出信息
1.2 操作
1.2.1 增加记录
由于主键自增长和状态初始化为“未启用”,所以只需要传voterecord的其他属性就可以了。详情见函数SAVESURVEY()
1.2.2 删除记录
只需要传递RecordID即可,详情见函数DELETESURVEY()
1.2.3 修改记录
问卷只有确定后才能被启用,所以修改记录在问卷设置好后用到的只是修改status,详见函数UPDATESURVEY()
1.2.4 查询记录
查询记录用到的比较多,只需要按RecordID把所有记录取出来就行了。详见函数READSURVEY()
1.2.5 代码补充
数据接口 名为survey的json数组
数据结构:
[{
"s": "end", //这个是status
"RecordID": "4",
"RecordName":"save survey test",
"describtion":"decribtion",
"welcome":"welcome",
"end":"thanks",
"starttime":"2019-4-1",
"endtime":"2019-4-3",
"method": "update" //这个是传递过来的方法,surveyCTL通过这个判断使用那个函数
}]
surveyCTL.php
<?php require_once('Connections/connjxkh.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
header("Content-Type: text/html;charset=utf-8");
mysql_query('SET NAMES utf8');
/**接收json数组*/
if(isset($_POST['survey'])){
$survey=json_decode($_POST['survey'],true);
}
mysql_select_db($database_connjxkh, $connjxkh);
/**取json数据**/
$json = file_get_contents('4.json');
$records=json_decode($json, true);
function SAVESURVEY($record,$connjxkh){
$sql ="INSERT INTO voterecord(RecordName,describtion,welcome,end,starttime,endtime,status) VALUES ('".$record["RecordName"]."','".$record["describtion"]
."','".$record["welcome"]."','".$record["end"]."','".$record["starttime"]."','".$record["endtime"]."','未启用')";
echo $sql;
if (mysql_query($sql, $connjxkh)) {
echo "\ntrue";
} else echo "\nfalse";
}
function READSURVEY($connjxkh){
$sql ="SELECT * FROM voterecord";
echo $sql."<br>";
$result = mysql_query($sql, $connjxkh);
while ($res = mysql_fetch_assoc($result)){
echo $res["RecordName"]." ".$res["starttime"]." ".$res["endtime"]."<br>";
}
}
function UPDATESURVEY($record,$connjxkh){
print_r($record);
$sql = "UPDATE voterecord SET status ='".$record["s"]."' WHERE RecordID = ".$record["RecordID"];
echo $sql;
if(mysql_query($sql, $connjxkh)){
echo "SUCCESSFUL";
}
else echo "ERROR";
}
function DELETESURVEY($record,$connjxkh){
$sql = "DELETE FROM voterecord WHERE RecordID = ".$record["RecordID"];
echo $sql."<br>";
if(mysql_query($sql, $connjxkh)){
echo "SUCCESSFUL";
}
else echo "ERROR";
$sql = "DELETE FROM question WHERE RecordID = ".$record["RecordID"];
echo $sql."<br>";
if(mysql_query($sql, $connjxkh)){
echo "SUCCESSFUL";
}
else echo "ERROR";
$sql = "DROP TABLE ".$record["RecordID"]."_answer";
echo $sql."<br>";
if(mysql_query($sql, $connjxkh)){
echo "SUCCESSFUL";
}
else echo "ERROR";
}
if(is_array($records)) {
foreach ($records as $record) {
switch ($record["method"]) {
case "save":
SAVESURVEY($record, $connjxkh);
break;
case "read":
READSURVEY($connjxkh);
break;
case "update":
UPDATESURVEY($record,$connjxkh);
break;
case "delete":
DELETESURVEY($record,$connjxkh);
break;
}
}
}
else echo "not array";
mysql_close($connjxkh);
?>
2 对question表的操作
2.1 question表的作用和结构
question 表存储问卷的所有问题和选项,如果为什么能存储搜偶问题和选项如果不太清楚,可以看一下我的上一篇文章。这里再稍微提一下,问题和选项使用同样的数据结构就可以了。
questionID 主键自增长 被answer表引用
parent_qid 存储的是该选项的问题questionID ,如果本身就是问题,则parent_qid就是0
type (单选题【问题 "S" 选项"1"】 多选题【问题 “D" 选项"2"】 文本问题【问题 "T" 回答"3"】)
title 问题或者选项的自主标号,如Q1 , A
question 问题或者选项值
2.2 question表的操作
question只需要新增和查询两个功能。删除和修改是前端里面完成的,不再后台处理。
2.2.1 增加问题或者选项
详见SAVEQUESTION()函数
2.2.2 查询问题或者选项
详见READQUESTION()函数
2.2.3 代码补充
数据接口: 名为question的json数组
数据结构:
{
"parent_qid":"",
"type":"0",
"title":"asdfas",
"question":"ljdfaiosjf",
"method": "read"
}
questionCTL.php
<?php require_once('Connections/connjxkh.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="css/layui.css"/>
</head>
<body>
<?php
if (!isset($_SESSION)) {
session_start();
}
mysql_query('SET NAMES UTF8');
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
header("Content-Type: text/html;charset=utf-8");
mysql_select_db($database_connjxkh, $connjxkh);
/**按Record搜索数据库*/
function Perparation($result)
{
while ($res = mysql_fetch_assoc($result)) {//取出表study_sql中的所有结果集
$parent_qid = $res["parent_qid"];
$type = $res["type"];
$title = $res["title"];
$question = $res["question"];
if ($parent_qid == 0) { //问题
global $cnt;
$cnt = 0;
//下面输出问题
echo "<h2>" . $title . " " . $question . "</h2><br>";
} else { //答案
//下面输出答案
if ($type == "T") { //问题为文本类型
echo "<textarea rows='3' cols='20'>";
} else { // 单选题多选题2个一排排列
global $cnt;
echo $title . " " . $question . " ";
if (!$cnt & 1) {
echo "<br>";
}
}
}
echo "</tr>";
}
}
function READQUESTION($connjxkh){
$_SESSION["RecordID"] = "3";
$sql = "SELECT RecordName FROM voterecord where RecordID =". $_SESSION["RecordID"];
$res = mysql_fetch_assoc(mysql_query($sql, $connjxkh));
echo "<h1>".$res["RecordName"]."</h1>"; // 调查名输出
$sql = "SELECT * FROM question where RecordID =" . $_SESSION["RecordID"];
$result = mysql_query($sql, $connjxkh);
Perparation($result);
}
function SAVEQUESTION($question,$connjxkh){
$sql = "SELECT MAX(RecordID) FROM voterecord";
$res = mysql_fetch_array(mysql_query($sql,$connjxkh));
$RecordID = $res["MAX(RecordID)"];
$_SESSION["RecordID"] = $RecordID;
/**取parent_qid*/
$sql = "SELECT MAX(questionID) FROM question where (RecordID =".$RecordID." and type !='T');";
$result = mysql_fetch_array(mysql_query($sql,$connjxkh));
$parent_qid = $result["MAX(questionID)"];
if($parent_qid == null){
$parent_qid = 0;
}
$title_array = $question["title"];
$question_array = $question["question"];
$type_array = $question["type"];
if($question["parent_qid"]!=null){
$parent_qid = "0";
}
$sql = "INSERT INTO question(parent_qid, RecordID, type, title, question) VALUES (";
$sql = $sql . $parent_qid . "," . $RecordID . ",'" . $type_array . "','" . $title_array . "','" . $question_array . "')";
echo $sql."<br>";
if (mysql_query($sql, $connjxkh)) {
echo "\ntrue";
} else echo "\nfalse";
}
/**取json数据**/
$json = file_get_contents('1.json');
$questions=json_decode($json, true);
print_r($questions);
if(is_array($questions)) {
foreach($questions as $question){
switch ($question["method"]) {
case "save":
SAVEQUESTION($question, $connjxkh);
break;
case "read":
READQUESTION($connjxkh);
break;
case "update":
UPDATEQUESTION($question,$connjxkh);
break;
case "delete":
DELETEQUESTION($question,$connjxkh);
break;
}
}
}
else echo "not array";
mysql_close($connjxkh);
?>
</body>
</html>