一、学生考试信息录入
1、首先数据库中要有两个表,student 、score;
表结构可以看一下
student 学生基本信息表
score 分数表
2、学生信息表中手动录入所有学生信息基本信息
3、展示学生考试成绩录入视图页面, 为了使用者录入学生考试成绩方便,首先查询出学生基本信息采用表格形式提交,代码如下:
<?php header("content-type:text/html;charset=utf-8"); $con=mysqli_connect('127.0.0.1','root','root','lening'); $sql="select * from student"; $res=mysqli_query($con,$sql); while($arr=mysqli_fetch_assoc($res)){ $data[]=$arr; }; foreach($data as $k=>$v){ $data[$k]['stu_age']=date('Y')-date('Y',strtotime($v['stu_age'])); } ?> <form action="score_do.php" method="post"> <table border="1"> <tr> <th>序号</th> <th>姓名</th> <th>入学时间</th> <th>性别</th> <th>年龄</th> <th>是否毕业</th> <th>分数</th> <th>考试时间</th> </tr> <?php foreach($data as $k=>$v){?> <tr> <td><?php echo $v['stu_id']?></td> <td><?php echo $v['stu_name']?></td> <td><?php echo $v['stu_time']?></td> <td><?php echo $v['stu_sex']?></td> <td><?php echo $v['stu_age']?></td> <td><?php echo $v['is_graduate']?></td> <td> <input type="hidden" value="<?php echo $v['stu_id']?>" name="stu_id[]"> <input type="text" placeholder="请输入分数" name="score[]"> </td> <td> <input type="date" placeholder="请输入考试时间" name="ctime[]"> </td> </tr> <?php }?> <tr> <td colspan="8"> <input type="submit" value="添加"> <input type="reset"> </td> </tr> </table> </form>
效果视图:
4、接值处理入库:
<?php header('content-type:text/html;charset=UTF-8'); include_once 'Db.class.php'; $arr=$_POST; if(!empty($arr)){ foreach($arr as $key=>$item){ foreach($item as $k=>$v){ $data[$k][$key]=$v; } } $db=new Db(); foreach($data as $k=>$v){ if($v['score']!=''&&$v['ctime']!=''){ $res=$db->add('score',$v); } } if($res){ echo "添加成功"; header("refresh:1;url='scoreList.php'"); }else{ echo "添加失败"; header("refresh:1;url='score.php'"); } }
5、入库后是列表展示
(1)列表展示中具有姓名查询功能
(2)分数段查询功能
<?php header('content-type:text/html;charset=utf8'); $con=mysqli_connect('127.0.0.1','root','root','lening'); $sql="select * from score join student on score.stu_id=student.stu_id"; $res=mysqli_query($con,$sql); while($arr=mysqli_fetch_assoc($res)){ $data[]=$arr; }; //print_r($data);exit; ?> <input type="text" id="keyword"> <input type="button" value="按姓名查找" onclick="search()"> <select id="type"> <option value="<"><</option> <option value="=">=</option> <option value=">">></option> </select> <select id="score"> <?php for($i=1;$i<101;$i++){?> <option value="<?php echo $i;?>"><?php echo $i;?></option> <?php }?> </select> <input type="button" value="按分数查找" onclick="scoreSearch()"> <table border="1" id="show"> <tr> <th>姓名</th> <th>分数</th> <th>考试时间</th> </tr> <?php foreach($data as $k=>$v){?> <tr> <td><?php echo $v['stu_name']?></td> <td><?php echo $v['score']?></td> <td><?php echo $v['ctime']?></td> </tr> <?php }?> </table>
查询功能运用ajax具体js代码如下:
<script> /** 按姓名查找 */ function search(){ var keyword=document.getElementById('keyword').value; //创建ajax对象 var xhr=new XMLHttpRequest(); //回调函数 xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ //接受响应 //console.log(xhr.responseText); document.getElementById('show').innerHTML=(xhr.responseText); } } //请求页面 xhr.open('get','search.php?stu_name='+keyword); //发送 xhr.send(null); } /** 按分数查找 */ function scoreSearch(){ var type=document.getElementById('type').value; var keyword=document.getElementById('score').value; //创建ajax对象 var xhr=new XMLHttpRequest(); //回调函数 xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ //接受响应 // console.log(xhr.responseText); document.getElementById('show').innerHTML=(xhr.responseText); } } //请求页面 xhr.open('get','searchScore.php?score='+keyword+'&type='+type); //发送 xhr.send(null); } </script>
serach.php页面:
<?php header("content-type:text/html;charset=utf-8"); $stu_name=empty($_GET['stu_name'])?"":$_GET['stu_name']; $con=mysqli_connect('127.0.0.1','root','root','lening') or die('链接数据库失败'); $sql="select * from student join score on score.stu_id=student.stu_id where stu_name='$stu_name'"; $res=mysqli_query($con,$sql); while($arr1=mysqli_fetch_assoc($res)){ $data[]=$arr1; } ?> <table border="1"> <tr> <td>姓名</td> <td>分数</td> <td>考试时间</td> </tr> <?php if(empty($data)){ echo "";}else{ foreach($data as $k=>$v){?> <tr> <td><?php echo $v['stu_name']?></td> <td><?php echo $v['score']?></td> <td><?php echo $v['ctime']?></td> </tr> <?php }}?> </table>
searchScore.php页面:
<?php header("content-type:text/html;charset=utf-8"); $score=empty($_GET['score'])?"":$_GET['score']; $type=empty($_GET['type'])?"":$_GET['type']; $con=mysqli_connect('127.0.0.1','root','root','lening') or die('链接数据库失败'); if($type=='<'){ $sql="select * from student join score on score.stu_id=student.stu_id where score<'$score'"; }else if($type=='>'){ $sql="select * from student join score on score.stu_id=student.stu_id where score>'$score'"; }else{ $sql="select * from student join score on score.stu_id=student.stu_id where score='$score'"; } $res=mysqli_query($con,$sql); while($arr1=mysqli_fetch_assoc($res)){ $data[]=$arr1; } ?> <table border="1"> <tr> <td>姓名</td> <td>分数</td> <td>考试时间</td> </tr> <?php if(empty($data)){ echo "";}else{ foreach($data as $k=>$v){?> <tr> <td><?php echo $v['stu_name']?></td> <td><?php echo $v['score']?></td> <td><?php echo $v['ctime']?></td> </tr> <?php }}?> </table>
效果图:
二、最后一次考试成绩查询
(1)时间筛选为最近一次
(2)两表联查,学生考试信息
代码:
<?php header("content-type:text/html;charset=utf-8"); //链接数据库 $con=mysqli_connect('127.0.0.1','root','root','lening')or die(mysqli_error()); //sql语句 $sqls = "select distinct stu_id from `score` "; $result=mysqli_query($con,$sqls); while($arr=mysqli_fetch_assoc($result)) { $data[]=$arr; } foreach( $data as $k=>$v){ $ids=$v['stu_id']; $sql = "select * from `student` join `score` on `score`.stu_id=`student`.stu_id where `score`.stu_id = $ids order by ctime desc limit 1 "; $res=mysqli_query($con,$sql); while($arr1=mysqli_fetch_assoc($res)) { $data1[]=$arr1; } } foreach($data1 as $k=>$v){ $data1[$k]['stu_age']=date('Y')-date('Y',strtotime($v['stu_age'])); } //print_r($data1);exit; ?> <table border="1"> <tr> <th>学生id</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>分数</th> <th>入学时间</th> <th>考试时间</th> <th>点击发送邮箱</th> </tr> <?php foreach ($data1 as $k => $v) { ?> <tr> <td><?php echo $v['stu_id'];?></td> <td><?php echo $v['stu_name'];?></td> <td><?php echo $v['stu_sex'];?></td> <td><?php echo $v['stu_age'];?></td> <td><?php echo $v['score'];?></td> <td><?php echo $v['stu_time'];?></td> <td><?php echo $v['ctime'];?></td> <td> <input type="button" value="发送家长" onclick="send(<?php echo $v['id']?>)"> </td> </tr> <?php } ?> </table>
效果图:
目前此博客优点是:
不足:
linux运行:
window运行: