PHP实例练习--投票和租房子
一,调查问卷
效果图:
整体的思路:
建立相应数据库->以复选框的形式显示列表->提交并处理数据(以主键的形式提交)->处理数据页面(修改数据的number值)->显示进度条(取数据库的number值)
代码:(自己写的还有好多细节没有实现到位)
投票页面:
这两个页面都引用了封装类(DBDA.php);
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>投票</title> 6 <style> 7 .xx{ 8 display:inline-block; 9 width:90px; 10 } 11 .dis{ 12 display:none; 13 } 14 .jdt{ 15 display:inline-block; 16 width:200px; 17 border:1px #333 solid; 18 height: 10px; 19 } 20 .jdtn{ 21 display:inline-block; 22 height: 10px; 23 width:0; 24 background:#090; 25 } 26 </style> 27 </head> 28 <body> 29 <div class="main"> 30 <?php 31 //引入数据库类 32 include("DBDA.php"); 33 //实例化数据库对象 34 $db=new DBDA(); 35 36 //查询题目数据库 37 $sql_tm="select * from dytm"; 38 $result_tm=$db->Query($sql_tm,1,"exam");//题目数据库数组 39 40 //查询选项数据库 41 $sql_xx="select * from dyxx where timudaihao='{$result_tm[0][0]}'"; 42 $result_xx=$db->Query($sql_xx,1,"exam");//选项数据库数组 43 echo "{$result_tm[0][1]}<br/>";//输出题目 44 ?> 45 <!--建立表单--> 46 <form action="voteTest.php" method="post"> 47 <?php 48 //以复选框的形式输出选项数据库 49 for($i=0;$i<count($result_xx);$i++){ 50 //计算已经选择的人数 51 $number=$result_xx[$i][2]*2; 52 53 echo "<span class='xx'>{$result_xx[$i][1]}</span> 54 <input type='checkbox' name='xx[]' value='{$result_xx[$i][1]}'/> 55 <div class='dis'><div class='jdt'><div class='jdtn' style='width:{$number}%'></div></div> 56 <span>{$result_xx[$i][2]}({$number}%)</span></div> 57 <br/>"; 58 } 59 ?> 60 61 <input type="submit" value="提交"/> 62 <input type="button" value="查看结果" onclick="dis()"/> 63 </form> 64 <!--表单结束--> 65 </div> 66 </body> 67 </html> 68 <script> 69 function dis(){ 70 var f=document.getElementsByClassName('dis'); 71 for(var i=0;i<f.length;i++){ 72 f[i].style.display="inline"; 73 } 74 75 } 76 </script>
投票处理页面:
1 <?php 2 //提交过来的数据 3 @$result=$_POST["xx"]; 4 /*var_dump($result);*/ 5 //选项数据库数据 6 include("DBDA.php"); 7 $db=new DBDA(); 8 $sql_xx="select * from dyxx"; 9 $result_xx=$db->Query($sql_xx,1,"exam"); 10 /*var_dump($result_xx);*/ 11 if(!empty($result)){ 12 //比较增加numbers 13 14 for($i=0;$i<count($result);$i++){ 15 16 $sql="select count(*) from dyxx where options='{$result[$i]}'"; 17 $num=$db->Query($sql,1,"exam"); 18 $sql1="select numbers from dyxx where options='{$result[$i]}'"; 19 $num1=$db->Query($sql1,1,"exam"); 20 21 if($num[0][0]==1){ 22 $num[0][0]+=$num1[0][0]; 23 $sql_xx1="update dyxx set numbers={$num[0][0]} where options='{$result[$i]}'"; 24 $db->Query($sql_xx1,0,"exam"); 25 }; 26 header("location:vote.php"); 27 } 28 }else{ 29 header("location:vote.php"); 30 } 31 32 33 ?>
二,租房子
效果图
查询页面:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>租房信息编辑页面</title> 6 <style> 7 a{ 8 text-decoration:none; 9 } 10 a:link,a:visited{ 11 color:#00F; 12 } 13 table{ 14 text-align:center; 15 } 16 span{ 17 color:#00F; 18 cursor:pointer; 19 } 20 </style> 21 </head> 22 23 <body> 24 <table width="100%" cellpadding="0" cellspacing="0" border="1"> 25 <tr><td>操作</td><td>关键字</td><td>区域</td><td>使用面积</td><td>租金</td><td>租赁类型</td><td>房屋类型</td></tr> 26 <?php 27 //连接数据库,取出数据,并在表格中显示 28 include("DBDA.php"); 29 $db=new DBDA(); 30 $sql="select * from house order by keyword"; 31 $result=$db->Query($sql,1,"housedb"); 32 //var_dump($result); 33 $strconfirm="\"确认删除?\""; 34 for($i=0;$i<count($result);$i++){ 35 36 echo "<tr> 37 <td> 38 <a href='AddInfo.php?bj=2&id={$result[$i][0]}'>编辑</a> 39 <a href='AddInfo.php?bj=3&id={$result[$i][0]}' 40 onclick='javascript:if(confirm({$strconfirm})){return ture}else{return false}' 41 >删除</a> 42 </td> 43 <td>{$result[$i][1]}</td> 44 <td>{$result[$i][2]}</td> 45 <td>{$result[$i][3]}</td> 46 <td>{$result[$i][4]}</td> 47 <td>{$result[$i][5]}</td> 48 <td>{$result[$i][6]}</td> 49 </tr>"; 50 } 51 ?> 52 </table> 53 <a href="AddInfo.php?bj=1"><input type="button" value="添加数据"/></a> 54 <a href="search.php"><input type="button" value="查询"/></a> 55 <script> 56 /*function Confirm(){ 57 var f=document.getElementsByClassName("tz"); 58 for(var i=0;i<f.length;i++){ 59 60 } 61 var bool=confirm("你确定删除?"); 62 alert(bool); 63 if(bool){ 64 window.location.href=""; 65 }; 66 }*/ 67 </script> 68 </body> 69 </html>
效果图
代码:
处理代码:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>处理界面</title> 6 </head> 7 <body> 8 <?php 9 include("DBDA.php"); 10 $bj=$_GET["bj"]; 11 $db=new DBDA(); 12 //$bj=1; 13 if($bj==1){ 14 $arradd=$_POST["sja"]; 15 //var_dump($arradd); 16 $str="'{$arradd[0]}','{$arradd[1]}',{$arradd[2]},{$arradd[3]},'{$arradd[4]}','{$arradd[5]}'"; 17 //echo $str."<br/>"; 18 $sql="INSERT INTO house(keyWord,area,squaremeter,rent,renttype,housetype) VALUES ({$str})"; 19 //echo $sql; 20 $bool=$db->Query($sql,0,"housedb"); 21 if($bool){ 22 header("location:HouseInfo.php"); 23 }else{ 24 header("location:AddInfo.php?bj=1;"); 25 } 26 27 }else if($bj==2){ 28 $arrupdate=$_POST["sja"]; 29 $id=$_GET["id"]; 30 $sql="delete from house where id={$id}"; 31 $bool=$db->Query($sql,0,"housedb"); 32 $stru="'{$arrupdate[0]}','{$arrupdate[1]}',{$arrupdate[2]},{$arrupdate[3]},'{$arrupdate[4]}','{$arrupdate[5]}'"; 33 $sqlu="INSERT INTO house(keyWord,area,squaremeter,rent,renttype,housetype) VALUES ({$stru})"; 34 $db->Query($sqlu,0,"housedb"); 35 header("location:HouseInfo.php"); 36 } 37 ?> 38 </body> 39 </html>
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>添加/修改/删除房屋数据</title> 6 <style> 7 a{ 8 text-decoration:none; 9 } 10 .text{ 11 display:inline-block; 12 width:100px; 13 text-align:center; 14 } 15 </style> 16 </head> 17 <body> 18 <?php 19 $bj=$_GET["bj"]; 20 //$bj=1; 21 include("DBDA.php"); 22 $db=new DBDA(); 23 //echo $bj; 24 if($bj==1){ 25 //添加数据 26 echo " 27 28 <form action='chuli.php?bj=1' method='post'> 29 <div class='text'>关键字:</div><input type='text' name='sja[]'/><br/> 30 <div class='text'>区域:</div><input type='text' name='sja[]'/><br/> 31 <div class='text'>使用面积:</div><input type='text' name='sja[]'/><br/> 32 <div class='text'>租金:</div><input type='text' name='sja[]'/><br/> 33 <div class='text'>租赁类型:</div><input type='text' name='sja[]'/><br/> 34 <div class='text'>房屋类型:</div><input type='text' name='sja[]'/><br/> 35 <input type='submit' value='插入'/> 36 <a href='HouseInfo.php'><input type='button' value='取消'/></a> 37 <form> 38 "; 39 }else if($bj==2){ 40 //修改数据 41 $idu=$_GET["id"]; 42 $sqlu="select * from house where id={$idu}"; 43 $arrUpdate=$db->Query($sqlu,1,"housedb"); 44 echo " 45 <form action='chuli.php?bj=2&id={$arrUpdate[0][0]}' method='post'> 46 <div class='text'>关键字</div><input type='text' name='sja[]' value='{$arrUpdate[0][1]}'/><br/> 47 <div class='text'>区域</div><input type='text' name='sja[]' value='{$arrUpdate[0][2]}'/><br/> 48 <div class='text'>使用面积</div><input type='text' name='sja[]' value='{$arrUpdate[0][3]}'/><br/> 49 <div class='text'>租金</div><input type='text' name='sja[]' value='{$arrUpdate[0][4]}'/><br/> 50 <div class='text'>租赁类型</div><input type='text' name='sja[]' value='{$arrUpdate[0][5]}'/><br/> 51 <div class='text'>房屋类型</div><input type='text' name='sja[]' value='{$arrUpdate[0][6]}'/><br/> 52 <input type='submit' value='更新'/> 53 <a href='HouseInfo.php'><input type='button' value='取消'/></a> 54 <form> 55 "; 56 }else if($bj==3){ 57 //删除数据 58 $id=$_GET["id"]; 59 $sql="delete from house where id={$id}"; 60 $bool=$db->Query($sql,0,"housedb"); 61 header("location:HouseInfo.php"); 62 63 64 } 65 66 67 ?> 68 69 </body> 70 </html>
查询效果:
代码:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>搜索</title> 6 <style> 7 a{ 8 text-decoration:none; 9 } 10 table{ 11 text-align:center; 12 } 13 </style> 14 </head> 15 16 <body> 17 <?php 18 include("DBDA.php"); 19 $db=new DBDA(); 20 ?> 21 <form action="search.php" method="post"> 22 区域: <input type="button" name="area" id="an1" onclick="selectAll('an1','area')" value="全选"/><br/> 23 <?php 24 $sql="select distinct area from house"; 25 $arrarea=$db->Query($sql,1,"housedb"); 26 //var_dump($arrarea); 27 for($i=0;$i<count($arrarea);$i++) 28 echo " 29 <input class='area' type='checkbox' name='area[]' value='{$arrarea[$i][0]}'/>{$arrarea[$i][0]} 30 "; 31 ?> 32 <br/><br/>租赁类型:<input type="button" id="an2" name="rent" onclick="selectAll('an2','rent')" value="全选"/><br/> 33 <?php 34 $sql="select distinct renttype from house"; 35 $arrrent=$db->Query($sql,1,"housedb"); 36 //var_dump($arrarea); 37 for($i=0;$i<count($arrrent);$i++) 38 echo " 39 <input class='rent' type='checkbox' name='rent[]' value='{$arrrent[$i][0]}'/>{$arrrent[$i][0]} 40 "; 41 ?> 42 <br/><br/>房屋类型:<input type="button" id="an3" name="house" onclick="selectAll('an3','house')" value="全选"/><br/> 43 <?php 44 $sql="select distinct housetype from house"; 45 $arrhouse=$db->Query($sql,1,"housedb"); 46 //var_dump($arrarea); 47 for($i=0;$i<count($arrhouse);$i++) 48 echo " 49 <input class='house' type='checkbox' name='house[]' value='{$arrhouse[$i][0]}'/>{$arrhouse[$i][0]} 50 "; 51 ?> 52 <br/><br/>关键字:<input type="text" name="search"/> 53 <br/> 54 <input id="an4" type="submit" value="搜索" onclick="display()"/><a href="HouseInfo.php"><input type="button" value="返回管理页面"/></a> 55 </form> 56 <!--显示--> 57 <div id="table"> 58 <table width="100%" cellpadding="0" cellspacing="0" border="1" > 59 <tr><td>关键字</td><td>区域</td><td>使用面积</td><td>租金</td><td>租赁类型</td><td>房屋类型</td></tr> 60 <?php 61 //连接数据库,取出数据,并在表格中显示 62 @$area=$_POST["area"];//这个地方应该用判断的 63 @$rent=$_POST["rent"]; 64 @$house=$_POST["house"]; 65 @$search=$_POST["search"]; 66 /*var_dump($area); 67 var_dump($rent); 68 var_dump($house); 69 var_dump($search);*/ 70 71 if(!empty($area)){ 72 $strarea=implode("','",$area); 73 }else{ 74 //var_dump ($arrarea); 75 $arr=changeArr($arrarea); 76 $strarea=implode("','",$arr); 77 }; 78 if(!empty($rent)){ 79 $strrent=implode("','",$rent); 80 }else{ 81 $arr=changeArr($arrrent); 82 $strrent=implode("','",$arr); 83 }; 84 if(!empty($house)){ 85 $strhouse=implode("','",$house); 86 }else{ 87 $arr=changeArr($arrhouse); 88 $strhouse=implode("','",$arr); 89 }; 90 if(empty($search)){ 91 $search='%'; 92 }; 93 94 /*$strrent=implode("','",$rent); 95 $strhouse=implode("','",$house);*/ 96 /*echo $strarea; 97 echo $strhouse; 98 echo $strrent;*/ 99 $sql="select * from house where area in ('{$strarea}') 100 and renttype in ('{$strrent}') 101 and housetype in ('{$strhouse}') 102 and keyword like '%{$search}%'"; 103 /*echo $sql; 104 $sql="select * from house";*/ 105 $result=$db->Query($sql,1,"housedb"); 106 //var_dump($result); 107 for($i=0;$i<count($result);$i++){ 108 109 echo "<tr>"; 110 for($j=1;$j<count($result[$i]);$j++){ 111 echo "<td>{$result[$i][$j]}</td>"; 112 } 113 echo "</tr>"; 114 } 115 ?> 116 </table> 117 </div> 118 <?php 119 function changeArr($arr2){ 120 $arr1=array(); 121 for($j=0;$j<count($arr2);$j++){ 122 array_push($arr1,$arr2[$j][0]); 123 } 124 return $arr1; 125 } 126 ?> 127 <script> 128 function selectAll(id1,class1){ 129 /*alert(arguments[0]); 130 alert(id1);*/ 131 var arr=document.getElementsByClassName(class1); 132 var f=document.getElementById(id1); 133 //alert(f.value); 134 if(f.value=="全选"){ 135 for(var i=0;i<arr.length;i++){ 136 arr[i].checked="checked"; 137 }; 138 f.value="全不选"; 139 }else{ 140 for(var i=0;i<arr.length;i++){ 141 arr[i].checked=""; 142 }; 143 f.value="全选"; 144 } 145 146 } 147 function display(){ 148 var f=document.getElementById("table"); 149 f.style.display="block"; 150 } 151 152 </script> 153 </body> 154 </html>