기록해! 정리해!

HTML --성적조회 프로그램Ver 1.0 (2) 코드정리 본문

Visual studio (PHP)

HTML --성적조회 프로그램Ver 1.0 (2) 코드정리

zsuling 2022. 6. 22. 09:49

º top.php

<html>

<head>
 <title> 정보처리기능사 연습 </title>
 <style>
header{
    background-color:blue;
    height:80px;
    line-height:80px;
    color:#ffffff;
    font-size:28px;
    text-align:center;
}
nav{
    background-color:#00aaff;
    height:30px;
    line-height:30px;
    color:#ffffff;
    font-size:14px;
}
section{
    background-color:#cccccc;
    min-height:650px;
}
footer{
    background-color:#0033ff;
    height: 40px;
    line-height:40px;
    color:#ffffff;
    text-align:center;
    font-size: 17px;
}
#section1{
    text-align:center;
    font-size:25px;
}
#body{
    width:80%;
    margin:auto;
}
 </style>
</head>

<body> 
  <div id="body">
<header> (과정평가형 정보처리기능사)성적조회 프로그램 Ver1.0 </header>
<nav> 
  &emsp;&emsp;<a href=form.php>성적입력</a>
  &emsp;<a href=list.php>성적조회</a>
  &emsp;<a href=member.php>회원가입</a>
  &emsp;<a href=member_list.php>회원조회</a>
  &emsp;<a href=join_list.php>테이블조인</a> 
  &emsp;<a href=index.php>홈으로</a> 
  
</nav>

º member.php

<? include "top.php" ?>

<?
$sno=$_GET['sno'];
?>


<style>
 table{
   width: 400px ;
   height:250px ; 
 } 
 .td1{
  text-align:center;
 }
</style>

<script>
  function ck1(){
    if (f1.sno.value == "") {
      alert("학번을 입력해 주세요");
      f1.sno.focus();
      return false;
    }else{
        alert("학생 성적이 입력 되었습니다.");  
    }
  }
</script>   


<script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
<script>
    //본 예제에서는 도로명 주소 표기 방식에 대한 법령에 따라, 내려오는 데이터를 조합하여 올바른 주소를 구성하는 방법을 설명합니다.
    function sample4_execDaumPostcode() {
        new daum.Postcode({
            oncomplete: function(data) {
                // 팝업에서 검색결과 항목을 클릭했을때 실행할 코드를 작성하는 부분.

                // 도로명 주소의 노출 규칙에 따라 주소를 표시한다.
                // 내려오는 변수가 값이 없는 경우엔 공백('')값을 가지므로, 이를 참고하여 분기 한다.
                var roadAddr = data.roadAddress; // 도로명 주소 변수
                var extraRoadAddr = ''; // 참고 항목 변수

                // 법정동명이 있을 경우 추가한다. (법정리는 제외)
                // 법정동의 경우 마지막 문자가 "동/로/가"로 끝난다.
                if(data.bname !== '' && /[동|로|가]$/g.test(data.bname)){
                    extraRoadAddr += data.bname;
                }
                // 건물명이 있고, 공동주택일 경우 추가한다.
                if(data.buildingName !== '' && data.apartment === 'Y'){
                   extraRoadAddr += (extraRoadAddr !== '' ? ', ' + data.buildingName : data.buildingName);
                }
                // 표시할 참고항목이 있을 경우, 괄호까지 추가한 최종 문자열을 만든다.
                if(extraRoadAddr !== ''){
                    extraRoadAddr = ' (' + extraRoadAddr + ')';
                }

                // 우편번호와 주소 정보를 해당 필드에 넣는다.
                document.getElementById('sample4_postcode').value = data.zonecode;
                document.getElementById("sample4_roadAddress").value = roadAddr;
                document.getElementById("sample4_jibunAddress").value = data.jibunAddress;
                
                // 참고항목 문자열이 있을 경우 해당 필드에 넣는다.
                if(roadAddr !== ''){
                    document.getElementById("sample4_extraAddress").value = extraRoadAddr;
                } else {
                    document.getElementById("sample4_extraAddress").value = '';
                }

                var guideTextBox = document.getElementById("guide");
                // 사용자가 '선택 안함'을 클릭한 경우, 예상 주소라는 표시를 해준다.
                if(data.autoRoadAddress) {
                    var expRoadAddr = data.autoRoadAddress + extraRoadAddr;
                    guideTextBox.innerHTML = '(예상 도로명 주소 : ' + expRoadAddr + ')';
                    guideTextBox.style.display = 'block';

                } else if(data.autoJibunAddress) {
                    var expJibunAddr = data.autoJibunAddress;
                    guideTextBox.innerHTML = '(예상 지번 주소 : ' + expJibunAddr + ')';
                    guideTextBox.style.display = 'block';
                } else {
                    guideTextBox.innerHTML = '';
                    guideTextBox.style.display = 'none';
                }
            }
        }).open();
    }
  </script>



<section> 
<br><br>
<div id=section1>   
 학생 정보 입력
</div>
<br>
<div align=center>
<form name=f1 action="member_ok.php"  onSubmit="return ck1()"
      method="post"
      enctype="multipart/form-data">
<table border=1>
<tr><td width=100 class="td1">학번</td><td><input  type=text  name=sno value="<?=$sno?>" ></td></tr>

<tr><td  class="td1" >주소 </td><td>
<input type="text" name="postcode" id="sample4_postcode" placeholder="우편번호">
<input type="button" onclick="sample4_execDaumPostcode()" value="우편번호 찾기"><br>
<input type="text" name="roadAddress" id="sample4_roadAddress" placeholder="도로명주소">
<input type="hidden" id="sample4_jibunAddress" placeholder="지번주소">
<span id="guide" style="color:#999;display:none"></span>
<input type="text"name="detailAddress" d="sample4_detailAddress" placeholder="상세주소">
<input type="text"name="detailAddress" id="sample4_extraAddress" placeholder="참고항목">
  </td></tr>

<tr><td  class="td1">사진</td><td  align=center><input  type=file  name=img ></td></tr>

<tr><td colspan=2 align=center><input  type=submit  value="저 장 하 기" ></td></tr>
</table>
</form> 
</div>                   
</section>

<? include "bottom.php" ?>

º member_list.php

<? include "top.php" ?>
<? include "DBConn.php" ?>
<?
 // 상위 5개의 레코드 나타내기
 $SQL = "select * from  member order by sno desc limit 0, 5" ;
 $result = $conn -> query($SQL);

 // 회원수 별칭잡아서 나타내기
 $SQLCount = "select count(*) as tc from  member " ;
 $resultCount = $conn -> query($SQLCount);
 $rowCount = $resultCount ->fetch_assoc();
?>

<style>
 table{
   width: 500px ;
 } 

 td{
   text-align:center; 
   height: 25px ;
 }
</style>
<section> 
<br>
<div id=section1>   
 회원 조회 목록 (<?=$rowCount['tc']?> : 명)
</div>
<br>
<div align=center> 
<table border=1>
<tr> <td>학번</td> <td>주소</td><td>파일명</td><td>사진</td>

</tr>
<?
 while( $row = $result ->fetch_assoc() ) {
  
?>

<tr> <td><?=$row['sno']?></td>     
     <td>
      <a href=member_edit.php?sno=<?=$row['sno']?>>
      <?=$row['addr']?>
      </a>
     </td>
     <td><?=$row['img']?> </td>     
     <td><img src=./img/<?=$row['img']?> width=50 height=50> </td>
</tr>
<? }  ?>

</table>
<br>
</div>                   
</section>

<? include "bottom.php" ?>

º member_edit.php

<? include "top.php"?>
<? include "DBConn.php"?>

<?

$sno=$_GET['sno'];

$SQL= "select * from member where sno='$sno'";
$result= $conn-> query($SQL);
$row=$result->fetch_assoc();
$sno= $row['sno'];
$addr= $row['addr'];
$img= $row['img'];
?>

<style>
    table{
        width:350px;
        height:250px;
    }
td{
    text-align:center;
}
</style>

<script>
  function ck1(){
    if(f1.sno.value==""){
    alert("학번을 입력해주세요");
    f1.sno.focus();
    return false;
    } else{
    alert("학생 성적이 정상적으로 입력되었습니다");
    }
  }

  function ck2(sno){
    alert("확인 : " + sno);
    location.href="member_delete.php?sno="+sno;
  }

</script>

<section>
     <br><br><div id=section1><b>학생 정보 수정하기</b></div>
     <br>
<div align=center>
<form name="f1" action="member_update.php" onSubmit="return ck1()"
      method="post" 
      enctype="multipart/form-data">


<table border=1>
<input type=hidden name=oldimg value=<?=$img?>>
<tr><td colspan=2 align=center width=400 height=150><img src=./img/<?=$img?>></td></tr>
<tr><td width=100>학번</td><td><input type=text name=sno value="<?=$sno?>"></td></tr>
<tr><td>주소</td><td><input type=text name=addr value="<?=$addr?>"></td></tr>
<tr><td>사진</td><td><input type=file name=img></td></tr>

<tr><td colspan=2 align=center>
    <input type=submit value="수정하기">
    <input type=button value="삭제하기" onClick="ck2('<?=$sno?>')">
</table>
</form>
</div> 
<br>
 
</section>
<? include "bottom.php"?>

º member_ok.php

<? include "DBConn.php" ?>
<?

 $sno=$_POST['sno'];
 $addr=$_POST['postcode'] . ":" . $_POST['roadAddress'] . ":" . $_POST['detailAddress'] . ":" . $_POST['detailAddress'];
 $mfile=$_FILES['img']['name']; // 파일명 ( 코난.png )
 $tmp=$_FILES['img']['tmp_name'];
 
 if ( $mfile =="" ){
    $mfile = 'space.png';
 } else {
    if (file_exists("./img/$mfile")) {
        // 파일의 이름 구하기
        $f_fname = basename($mfile,strrchr($mfile,'.'));
        // 시간 가져오기 (시분초)
        $time = date("His", time());
        // 확장자 가져오기
        $ext = strrchr($mfile,'.') ;
        $mfile = $f_fname."_".$time.$ext ;
        move_uploaded_file($tmp, "./img/$mfile");
    }else{
        move_uploaded_file($tmp, "./img/$mfile");
    }
 }

 $SQL ="insert into member(sno,addr,img) values('$sno','$addr','$mfile')" ;
 $conn -> query( $SQL);
?>
<script>
 location.href="member_list.php" ;
</script>

º member_update.php

<? include "DBconn.php"?>

<?
$sno=$_POST['sno'];
$addr=$_POST['addr'];
$oldimg=$_POST['oldimg'];
$mfile=$_FILES['img']['name']; 
$tmp=$_FILES['img']['tmp_name']; 



$SQL = "select * from member where sno='$sno'";
$result = $conn -> query($SQL);
$row = $result -> fetch_assoc();
$img = $row['img'];


if($mfile==""){
    //1
    //파일 첨부가 없을 때 실행
    $SQL = "update member set addr='$addr', img='$mfile' where sno='$sno'";
    $conn -> query($SQL);
}
    
else{
    //2
    //파일 첨부가 있으면 실행
    if($oldimg=="space.png"){
     
        if(file_exists("./img/$mfile")){
            //2-1  
            //기존에 파일이 존재하면 실행
            $f_fname = basename($mfile,strrchr($mfile,'.')); 
            $time = date("His",time()); 
            $ext = strrchr($mfile,'.');
            $mfile= $f_fname."_".$time.$ext;
            move_uploaded_file($tmp, "./img/$mfile");
            $SQL = "update member set addr='$addr', img='$mfile' where sno='$sno'";
            $conn -> query($SQL); 
        }
        else{
            move_uploaded_file($tmp, "./img/$mfile");
            $SQL = "update member set addr='$addr', img='$mfile' where sno='$sno'";
            $conn -> query($SQL);  
        }
          
    }    
    else{
      //2-2  
      //기존에 파일이 존재하지 않으면 실행 
      if($img != 'space.png'){
        unlink("./img/$img");
        }
        if(file_exists("./img/$mfile")){
            $f_fname = basename($mfile,strrchr($mfile,'.'));
            $time = date("His",time()); 
            $ext = strrchr($mfile,'.'); 
            $mfile= $f_fname."_".$time.$ext;
            move_uploaded_file($tmp, "./img/$mfile");
            $SQL = "update member set addr='$addr', img='$mfile' where sno='$sno'";
            $conn -> query($SQL);
          }
          else{
            move_uploaded_file($tmp, "./img/$mfile");
            $SQL = "update member set addr='$addr', img='$mfile' where sno='$sno'";
            $conn -> query($SQL);
          }
    }
   
}


?>

<script>
     location.href="member_list.php" ;
</script>

º member_delete.php

<? include "DBConn.php" ?>
<?
  $sno = $_GET['sno'];
 
  $SQL = "select * from member where sno='$sno'";
  $result = $conn -> query($SQL);
  $row = $result -> fetch_assoc();
  $img = $row['img'];

  if ($img != 'space.png' ){
   unlink("./img/$img");
}
 
$SQL ="delete from member where sno ='$sno' " ;
$conn -> query($SQL);
?>
<script>
 location.href="member_list.php" ;
</script>

º oldmember_ok.php

<? include "DBConn.php" ?>
<?
  $sno = $_REQUEST['sno'];
  $addr = $_REQUEST['addr'];

  $mfile = $_FILES['img']['name'];
  $tmp = $_FILES['img']['tmp_name'];

  if ( $mfile =="") {
    $mfile='space.jpg' ; 
  } else {
    if(file_exists("./img/$mfile")) {
       $f_fname = basename($mfile, strrchr($mfile,"."));
       $randomNum = date("His",time());
       $fileinfo = pathinfo($mfile);
       $ext = $fileinfo['extension'];
       $mfile = $f_fname."_".$randomNum.".".$ext;
       move_uploaded_file($tmp, "./img/$mfile");
    } else{
       move_uploaded_file($tmp, "./img/$mfile");
    }
  }

$SQL ="insert  into  member (sno, addr, img) " ;
$SQL = $SQL . " values ('$sno','$addr' ,'$mfile')" ;

$conn -> query($SQL);
echo $SQL;
?>
<script>
// location.href="list.php" ;
</script>

º join_list.php

<? include "top.php"?>
<? include "DBconn.php" ?>
<?
$ch1=""; $ch2="";
$ch1 = $_GET['ch1'];
$ch2 = $_GET['ch2'];

$SQL = " select sno, sname, kor, eng, math, hist , " ;
$SQL = $SQL . " kor+eng+math+hist as ssum, " ;
$SQL = $SQL . " round((kor+eng+math+hist)/4,1) as savg from examtbl " ;

if( $ch1 == "" ){
    $SQL = $SQL . " order by sno desc " ;
    
}else if( $ch1 == "sno"){
    $SQL = $SQL . " where sno like '%$ch2%' order by sno desc " ;
    
}else if( $ch1 == "sname"){
    $SQL = $SQL . " where sname like '%$ch2%' order by sno desc " ;
    
}else if( $ch1 == "kor"){
    $SQL = $SQL . " where kor like '%$ch2%' order by sno desc " ;
}
 $result =  $conn -> query($SQL);
?>

<style>
    table{
        width:500px;
    }
    td{
        text-align:center;
        height:25px;
    }
</style>
<section>
    <br>
     <div id="section1">
      성적조회목록 <br>
     </div>
    <br>
    <div align=center>
    <table border=1>
<tr><td>학번</td> <td>이름</td> <td>국어</td> 
    <td>영어</td> <td>수학</td> <td>역사</td>
    <td>합계</td> <td>평균</td> <td>평점</td>
 </tr>

<? 
 $i =0;
 $s_kor=0;  $s_eng=0;  $s_math=0;  $s_hist=0;
 $s_sum=0;  $s_avg=0; 
while ($row = $result -> fetch_assoc()) {?>
<? 
    if ($row['savg']>=90){
        $grade = 'A';
    }else if ($row['savg']>=80){
        $grade = 'B';
    }else if($row['savg']>=70){
        $grade = 'C';
    }else{
        $grade = 'F';
    }    
    $i = $i + 1 ;

    $s_kor=$s_kor+$row['kor'];
    $s_eng=$s_eng+$row['eng'];
    $s_math=$s_math+$row['math'];
    $s_hist=$s_hist+$row['hist'];
    $s_sum=$s_sum+$row['ssum'];
    $s_avg=$s_avg+$row['savg'];
?>

<tr>
    <td><?=$row['sno']?></td>
    <td>
     <a href=edit.php?sno=<?=$row['sno']?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>
     <?=$row['sname']?></a></td>
    <td><?=$row['kor']?></td> 
    <td><?=$row['eng']?></td>
    <td><?=$row['math']?></td> 
    <td><?=$row['hist']?></td>
    <td><?=$row['ssum']?></td>
    <td><?=$row['savg']?></td>
    <td><?=$grade?></td>
</tr>
<?}?>
<tr>
     <td> &nbsp;  </td>
       <td> 총점 </td>
     <td><?=$s_kor?> </td>      <td><?=$s_eng?> </td>
     <td><?=$s_math?></td>      <td><?=$s_hist?> </td>
     <td><?=$s_sum?> </td>      <td><?=$s_avg?> </td>
     <td></td>
</tr>
<tr>
     <td> &nbsp;  </td>
       <td> 평균 (<?=$i?>명) </td>
     <td><?=round($s_kor / $i, 1)?> </td>      <td><?=round($s_eng / $i, 1)?> </td>
     <td><?=round($s_math / $i, 1)?></td>      <td><?=round($s_hist / $i, 1)?> </td>
     <td><?=round($s_sum / $i, 1)?> </td>      <td><?=round($s_avg / $i, 1)?> </td>
     <td></td>
</tr>    
</table>
<form>
    <select name=ch1>
        <option value="sno">학번</option>
        <option value="sname">이름</option>
        <option value="kor">국어점수</option>
</select>
<input type=text name=ch2>
<input type=submit value="검색하기">
</form>
</div>
</section>
<? include "bottom.php"?>

 

Comments