欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用php+mysql+xml完成一个调查问卷

程序员文章站 2022-03-18 16:44:28
本人根据php典型模块与项目实战大全此书所完成的一个调查问卷,同时管理员可以进行修改调查内容 同时用到了一个css文件,借鉴于 http://www.wufangbo.com/div-css-vote/ 需要5个php文件:admin.php、update.php、view.php、result.p ......

本人根据php典型模块与项目实战大全此书所完成的一个调查问卷,同时管理员可以进行修改调查内容

同时用到了一个css文件,借鉴于

http://www.wufangbo.com/div-css-vote/


 

需要5个php文件:admin.php、update.php、view.php、result.php、vote.php、

首先在www下新建一个文件夹 此处为名为"diaocha" 本人的端口号为8080

数据库如下:库的名字为 cms_vote 表的名字为:vote

使用php+mysql+xml完成一个调查问卷

 

 

其中votenote内部的内容为:

<movies>
<movie>
<title>您想去哪个城市:</title>
<id>1</id>
<count>350</count>
<content>广东</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>2</id>
<count>47</count>
<content>湖南</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>3</id>
<count>118</count>
<content>上海</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>4</id>
<count>122</count>
<content>北京</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>5</id>
<count>80</count>
<content>湖北</content>
</movie>
</movies>


 

1、admin.php

<?php
$con = mysql_connect('localhost','root','root')or die("could not connect database");//此处根据自己的数据库的名字,密码进行修改
mysql_query("set names utf8");
mysql_select_db('cms_vote')or die("could not select database");
$aid = $_get['aid'];
if(!empty($aid)){
$sql='select *from vote ';
//where aid=".$aid."';
$result = mysql_query($sql,$con);
$row = mysql_fetch_array($result);
}
else{
echo '<script>alert(\'调查异常\');</script>';
exit;
}
?>
<html>
<head>
<table width="98%" border=0 align="center" cellpadding="3" cellpadding="1">
<tr>
<td height="28" ><b>调查管理</b></td>
</tr>
<tr height="200" valign="top">
<form name="form1" method="post" action="update.php">
<input type="hidden" name = "aid" value="<?php echo $aid?>">
<table width="100%" border="0"cellpadding="4" cellspacing="4">
<tr>
<td width="15%" align="center">调查项目:</td>
<td width="85%">
<input name="votename" type="text" id="votename" value="<?php echo $row['votename'];?>">
</td>
</tr>

<tr>
<td align="center">调查总人数:</td>

<td>
<input name="totalcount" type="text" id="totalcount" value="<?php echo $row['totalcount'];?>">
</td>

</tr>


<tr>
<td align="center">开始时间:</td>
<td>
<input name="starttime" type="text" id="starttime" value="<?php echo $row['starttime'];?>">
</td>
</tr>

<tr>
<td align="center">结束时间:</td>
<td>
<input name="endtime" type="text" id="endtime" value="<?php echo $row['endtime'];?>">
</td>
</tr>


<tr>
<td align="center">投票项:</td>
<td>
<textarea name="votenote" rows="8" id ="votenote" style="width:80%">
<movies>
<movie>
<title>您想去哪个城市:</title>
<id>1</id>
<count>350</count>
<content>广东</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>2</id>
<count>47</count>
<content>湖南</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>3</id>
<count>118</count>
<content>上海</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>4</id>
<count>122</count>
<content>北京</content>
</movie>
<movie>
<title>您想去哪个城市:</title>
<id>5</id>
<count>80</count>
<content>湖北</content>
</movie>
</movies></textarea></td>
</tr>

<tr>
<td height="47">&nbsp;</td>
<td><input type="submit" name="submit" value="保存调查数据">
</td>
</tr>

<tr>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</form>

</tr>
</head>
</html>


2、update.php


<?php
$con = mysql_connect('localhost','root','root')or die("could not connect database");//此处根据自己的数据库的名字,密码进行修改
mysql_query("set names utf8");
mysql_select_db('cms_vote')or die("could not select database");
$aid = $_post['aid'];
$votename=$_post['votename'];
$starttime=$_post['starttime'];
$endtime=$_post['endtime'];
$votenote=$_post['votenote'];
$totalcount=$_post['totalcount'];

if(!empty($aid))
{
$sql = "update vote set votename= '".$votename."', starttime='".$starttime."', endtime='".$endtime."', totalcount='".$totalcount."', votenote='".$votenote."' where aid='".$aid."'order by aid desc limit 1;";
$result = mysql_query($sql);
if(!empty($result)){

echo '<script>alert(\'数据保存成功\');</script>';
echo "您的调查问卷已生效!";

}
else
{
echo '<script>alert(\'数据保存失败\');</script>';
exit();
}
}
else{
echo '<script>alert(\'调查异常\');</script>';
exit();
}
?>

验证是否成功:http://localhost:8080/diaocha/admin.php?aid=1

(注意需要加上?aod=1)格式为英文格式

使用php+mysql+xml完成一个调查问卷

按下保存调查数据按键的时候:会出现

使用php+mysql+xml完成一个调查问卷

会出现您的调查问卷已生效

 

 


 

 3、view.php

<?php
$voteitem = $_post['voteitem'];
$con = mysql_connect('localhost','root','root')or die("could not connect database");//密码用户名按照自己的修改
mysql_query("set names utf8");
mysql_select_db('cms_vote')or die("could not select database");
$sql="select *from vote";
$result = mysql_query($sql,$con);
$arr = mysql_fetch_array($result);
$xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlstr.=$arr['votenote'];
$xml = simplexml_load_string($xmlstr);
$b[]=array();
$a[]=array();
for($i=0;$i<5;$i++){
$b[$i]=$xml->movie[$i]->count;
$a[$i]=$xml->movie[$i]->count+1;
}
for($i=0;$i<5;$i++){
$b[$i]=$a[$i];
$xmlstra[$i]="<movies>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>1</id>
<count>".$b[0]."</count>
<content>". $xml->movie[0]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>2</id>
<count>".$b[1]."</count>
<content>". $xml->movie[1]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>3</id>
<count>".$b[2]."</count>
<content>". $xml->movie[2]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[3]."</count>
<content>". $xml->movie[3]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[4]."</count>
<content>". $xml->movie[4]->content ."</content>
</movie>
</movies>";
}

if($voteitem!=null){
$sql = "update vote set totalcount=totalcount+1,votenote='".$xmlstra[$voteitem]."' order by aid desc limit 1";
$result = mysql_query($sql);
$url = 'result.php?aid=1';
switch($voteitem){
case 0:
{
if(!empty($result)){
echo '<script>alert("投票1成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 1:
{
if(!empty($result)){
echo '<script>alert("投票2成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 2:
{
if(!empty($result)){
echo '<script>alert("投票3成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 3:
{
if(!empty($result)){
echo '<script>alert("投票4成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 4:
{
if(!empty($result)){
echo '<script>alert("投票5成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
}
}
else{
echo '<script>window.close();</script>';
}
?>

 会出现以下结果

使用php+mysql+xml完成一个调查问卷


 

 

 4、vote.php

<?php
$voteitem = $_post['voteitem'];
$con = mysql_connect('localhost','root','root')or die("could not connect database");
mysql_query("set names utf8");
mysql_select_db('cms_vote')or die("could not select database");
$sql="select *from vote";
$result = mysql_query($sql,$con);
$arr = mysql_fetch_array($result);
$xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlstr.=$arr['votenote'];
$xml = simplexml_load_string($xmlstr);
$b[]=array();
$a[]=array();
for($i=0;$i<5;$i++){
$b[$i]=$xml->movie[$i]->count;
$a[$i]=$xml->movie[$i]->count+1;
}
for($i=0;$i<5;$i++){
$b[$i]=$a[$i];
$xmlstra[$i]="<movies>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>1</id>
<count>".$b[0]."</count>
<content>". $xml->movie[0]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>2</id>
<count>".$b[1]."</count>
<content>". $xml->movie[1]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>3</id>
<count>".$b[2]."</count>
<content>". $xml->movie[2]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[3]."</count>
<content>". $xml->movie[3]->content ."</content>
</movie>
<movie>
<title>". $xml->movie[0]->title ."</title>
<id>4</id>
<count>".$b[4]."</count>
<content>". $xml->movie[4]->content ."</content>
</movie>
</movies>";
}

if($voteitem!=null){
$sql = "update vote set totalcount=totalcount+1,votenote='".$xmlstra[$voteitem]."' order by aid desc limit 1";
$result = mysql_query($sql);
$url = 'result.php?aid=1';
switch($voteitem){
case 0:
{
if(!empty($result)){
echo '<script>alert("投票1成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 1:
{
if(!empty($result)){
echo '<script>alert("投票2成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 2:
{
if(!empty($result)){
echo '<script>alert("投票3成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 3:
{
if(!empty($result)){
echo '<script>alert("投票4成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
case 4:
{
if(!empty($result)){
echo '<script>alert("投票5成功!");location.href="'.$url.'"</script>';
exit();
}
break;
}
}
}
else{
echo '<script>window.close();</script>';
}
?>


 

5、result.php

<?php
$con = mysql_connect('localhost','root','root')or die("could not connect database");
mysql_query("set names utf8");
mysql_select_db('cms_vote')or die("could not select database");
$aid = $_get['aid'];
if(!empty($aid)){
$sql="select *from vote where aid='".$aid."'";
$result = mysql_query($sql,$con);
$arr = mysql_fetch_array($result);
$xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlstr.=$arr['votenote'];
$xml = simplexml_load_string($xmlstr);
$n0=$xml->movie[0]->count/$arr['totalcount'];
$n1=$xml->movie[1]->count/$arr['totalcount'];
$n2=$xml->movie[2]->count/$arr['totalcount'];
$n3=$xml->movie[3]->count/$arr['totalcount'];
$n4=$xml->movie[4]->count/$arr['totalcount'];
}
/*else
{
echo '<script>alert(\'调查异常\');</script>';
exit;
}*/

?>
<html>
<head>
<style >
#graphbox{
border:1px solid #e7e7e7;
padding:10px;
width:545px;
background-color:#f8f8f8;
margin:5px 0;//这是最大的一个div
}
.itemname{
width:70px;
font-weight:700;
font-size:14px;
line-height:18px;
height:18px;
padding:2px;
text-align:right;
margin-right:atuo;
}
.percent{
width:150px;
float:right;
font-size:13px;
line-height:18px;
height:18px;
padding:2px;
color:#555;
text-align:left;
margin-right:3px;
}
.graph{
position:relative;
background-color:#f0efef;
border:1px solid #cccccc;
font-size:13px;
width:300px;
font-weight:700;
float:right;
margin-right:3px;
}
.color1, .color2, .color3, .color4, .color5{
position:relative;
text-align:left;
color:#ffffff;
height:18px;
display:block;
}
.graph .color1{background-color:#afb4db;}
.graph .color2{background-color:#84bf96;}
.graph .color3{background-color:#ea66a6;}
.graph .color4{background-color:#50b7c1;}
.graph .color5{background-color:#ffd400;}
.font1{color:#669999;}
.font2{color:#6699ff;}
.font3{color:#ff9900;}
.font4{color:#ff3333;}
</style>
<body>
<h3><?php echo $arr['votename']; ?></h3>
<td>调查结果</td>
<div>
<td>调查开始时间:<?php echo $arr['starttime']; ?> 调查结束时间:<?php echo $arr['endtime']; ?> 调查总人数:<?php echo $arr['totalcount']; ?></td></td>
<div>
<div id="graphbox">

<div class="little_box">
<div class="percent"><?php echo sprintf("%01.0f",$n0*100).'%';?> <?php echo $xml->movie[0]->count;?></div>
<div class="graph">
<span class="color1" style="width:<?php echo sprintf("%01.0f",$n0*100).'%';?>">&nbsp;</span></div>
<div class="itemname font1"><?php echo $xml->movie[0]->content;?></div>
</div>

<div class="little_box">
<div class="percent"><?php echo sprintf("%01.0f",$n1*100).'%';?> <?php echo $xml->movie[1]->count;?></div>
<div class="graph">
<span class="color2" style="width:<?php echo sprintf("%01.0f",$n1*100).'%';?>">&nbsp;</span></div>
<div class="itemname font2"><?php echo $xml->movie[1]->content;?></div>
</div>

<div class="little_box">
<div class="percent"><?php echo sprintf("%01.0f",$n2*100).'%';?> <?php echo $xml->movie[2]->count;?> </div>
<div class="graph">
<span class="color3" style="width:<?php echo sprintf("%01.0f",$n2*100).'%';?>;">&nbsp;</span></div>
<div class="itemname font3"><?php echo $xml->movie[2]->content;?></div>
</div>

<div class="little_box">
<div class="percent"><?php echo sprintf("%01.0f",$n3*100).'%';?> <?php echo $xml->movie[3]->count;?> </div>
<div class="graph">
<span class="color4" style="width:<?php echo sprintf("%01.0f",$n3*100).'%';?>;">&nbsp;</span></div>
<div class="itemname font4"><?php echo $xml->movie[3]->content;?></div>
</div>

<div class="little_box">
<div class="percent"><?php echo sprintf("%01.0f",$n4*100).'%';?> <?php echo $xml->movie[4]->count;?> </div>
<div class="graph">
<span class="color5" style="width:<?php echo sprintf("%01.0f",$n4*100).'%';?>;">&nbsp;</span></div>
<div class="itemname font5"><?php echo $xml->movie[4]->content;?></div>
</div>

</div>
</body>
</head>
</html>


该实例还没有完全实现更多的功能。后续会补充~嘻嘻嘻