본문 바로가기

비주얼스튜디오코드

[ 18일차 ] 비주얼스튜디오 코드

 

    F학점 학생만 빨간색으로 표시하기 
    
    $bgcolor="#cccccc";

    if ($avg >=90) {
        $grade = "A";
    } else if ($avg >= 80) {
        $grade = "B";
    } else if ($avg >=70) {
        $grade = "C";
    } else{
         $grade = "F";
         $bgcolor="#cccccc";
    }
    
?>
<tr bgcolor="<?=$bgcolor?>" >

 

 

 

top.php , bottom.php, DBconn.php form.php 파일 만들어서 나눠서 저장하기 

 

list.php 파일 

<? include 'top.php'?>
<? include 'DBconn.php'?>

<section>
<div align=center>
<br>
<h2> 학생 성적 목록 보기 </h2>
<?
$servername = "localhost";
$username = "root";
$password = "autoset";
$dbname = "ppk400";

// 1.서버 및 데이터베이스 컨넥션
$conn = new mysqli($servername, $username, $password, $dbname);

//2. 쿼리문(sql문)작성하기
$SQL = "select sno, sname, kor, eng, math, hist from examtbl" ; 

//3.쿼리문 실행하기
$result = $conn -> query($SQL); 
?>
<table border = 1>
<tr>
   <td> 순번</td><td> 학번</td> <td> 이름 </td> <td>국어 </td> <td>영어 </td> <td> 수학</td><td>역사 </td><td> 합계 </td><td>평균 </td></td><td>평점 </td>
</tr>
<?
$ksum =0; $esum =0; $msum =0; $hsum =0; $ssum =0; $asum =0; $count=0; $no=0;
// 4. while문에서 컬럼 출력하기
while ( $row = $result -> fetch_assoc() ){
    $sum = $row['kor'] + $row['eng'] + $row['math'] + $row['hist'];
    $avg = $sum / 4 ;

    $count = $count + 1;
    $no = $no+1;
    
    
    $ksum = $ksum + $row['kor'];
    $esum = $esum + $row['eng'];
    $msum = $msum + $row['math'];
    $hsum = $hsum + $row['hist'];
    $ssum = $ssum + $sum;
    $asum = $asum + $avg;

    $kavg = $ksum / $count;
    $eavg = $esum / $count;
    $mavg = $msum / $count;
    $havg = $hsum / $count;
    $savg = $ssum / $count;
    $aavg = $asum / $count;

    $bgcolor="#ffe4e1";

    if ($avg >=90) {
        $grade = "A";
    } else if ($avg >= 80) {
        $grade = "B";
    } else if ($avg >=70) {
        $grade = "C";
    } else{
         $grade = "F";
         $bgcolor="#ff0000";
    }
    
?>
<tr bgcolor="<?=$bgcolor?>" >
    <td><?=$no?> </td>
    <td><?= $row['sno'] ?> </td> 
    <td><?= $row['sname'] ?> </td>
    <td><?= $row['kor'] ?> </td>
    <td><?= $row['eng'] ?> </td>
    <td><?= $row['math'] ?> </td>
    <td><?= $row['hist'] ?> </td> 
    <td><?= $sum?> </td> 
    <td><?= $avg?> </td>
    <td><?= $grade?> </td> 
 </tr>
<?
}
?>
<tr>
    <td colspan=3> &nbsp;누적합</td> 
    <td><?= $ksum?> </td>
    <td><?= $esum?> </td>
    <td><?= $msum?> </td>
    <td><?= $hsum?> </td>
    <td><?= $ssum?> </td> 
    <td><?= $asum?> </td>
    <td> </td>
 </tr>

 <tr>
    <td colspan=3> &nbsp;누적평균(<?=$count?>명)</td> 
    <td><?= $kavg?> </td>
    <td><?= $eavg?> </td>
    <td><?= $mavg?> </td>
    <td><?= $havg?> </td>
    <td><?= $savg?> </td> 
    <td><?= $aavg?> </td>
    <td> </td>
 </tr>
</table>
</div>
<br>
</section>

<? include 'bottom.php'?>

 

DBconn.php 파일 

<? 
$servername = "localhost";
$username = "root";
$password = "autoset";
$dbname = "ppk400";

// 1.서버 및 데이터베이스 컨넥션
$conn = new mysqli($servername, $username, $password, $dbname);

?>

 

form.php 파일 


<? include 'top.php'?>
<? include 'DBconn.php'?>

<section>
<br>
<div align=center>
<h2> 학생 성적 입력 </h2>

</div>
<br>
</section>

<? include 'bottom.php'?>

 

top.php  파일 


<style> 
header{
    background-color:#ffb6c1;
    height:50px;
    line-height:50px;
    font-size:20px; color: #ffffff ;
    text-align: center ;

}

nav{
    background-color:#fffacd;
    height:30px;
    line-height:30px;
    font-size:13px; color: #ffffff ;

}

section{
    background-color:#ffe4e1;
    font-size:12px;
    min-height: 500px;

}

footer{
    background-color:#ffb6c1;
    height:40px;
    line-height:20px;
    font-size:14px; color: #ffffff ;
    text-align: center ;

}

table{
    width: 500px;

}

</style>

<header> 학생 성적처리 Ver 11. </header>
<nav>
 &emsp;&emsp;&emsp;<a href=form.php> 성적입력 </a> 
 &emsp;<a href=list.php> 성적조회 </a>
 &emsp;<a href=index.php> 홈으로 </a>
</nav>
bottom.php 파일 

<footer>
<address>
<center>
Home page : http://www.visiongongjang.com <br>
mobile : 010-2222-3333 <br>
</center>
</address>
</footer>

 

index.php 파일 수정하기 

<? include 'top.php'?>
<? include 'DBconn.php'?>

<section>
<br>
<div align=center>
<h2> 학생 성적 처리 프로그램요약 </h2>

</div>
<br>
</section>

<? include 'bottom.php'?>

 

결과 화

 

성적입력
성적조회
홈으로

 


CSS 해보기 

 

list.php 파일



<? include 'top.php'?>
<? include 'DBconn.php'?>

<style>

    table{
         width:600px;       // 테이블 내용 추가 
    }
</style>

<section>
<div align=center>
<br>
<h2> 학생 성적 목록 보기 </h2>
<?
$servername = "localhost";
$username = "root";
$password = "autoset";
$dbname = "ppk400";

// 1.서버 및 데이터베이스 컨넥션
$conn = new mysqli($servername, $username, $password, $dbname);

//2. 쿼리문(sql문)작성하기
$SQL = "select sno, sname, kor, eng, math, hist from examtbl" ; 

//3.쿼리문 실행하기
$result = $conn -> query($SQL); 
?>
<table border = 1>
<tr>
   <td> 순번</td><td> 학번</td> <td> 이름 </td> <td>국어 </td> 
   <td>영어 </td> <td> 수학</td><td>역사 </td><td> 합계 </td><td>평균 </td>
   <td>평점 </td>
</tr>
<?
$ksum =0; $esum =0; $msum =0; $hsum =0; $ssum =0; $asum =0; $count=0; $no=0;
// 4. while문에서 컬럼 출력하기
while ( $row = $result -> fetch_assoc() ){
    $sum = $row['kor'] + $row['eng'] + $row['math'] + $row['hist'];
    $avg = $sum / 4 ;

    $count = $count + 1;
    $no = $no+1;
    
    
    $ksum = $ksum + $row['kor'];
    $esum = $esum + $row['eng'];
    $msum = $msum + $row['math'];
    $hsum = $hsum + $row['hist'];
    $ssum = $ssum + $sum;
    $asum = $asum + $avg;

    $kavg = $ksum / $count;
    $eavg = $esum / $count;
    $mavg = $msum / $count;
    $havg = $hsum / $count;
    $savg = $ssum / $count;
    $aavg = $asum / $count;

    $bgcolor="#ffe4e1";

    if ($avg >=90) {
        $grade = "A";
    } else if ($avg >= 80) {
        $grade = "B";
    } else if ($avg >=70) {
        $grade = "C";
    } else{
         $grade = "F";
         $bgcolor="#ff0000";
    }
    
?>
<tr bgcolor="<?=$bgcolor?>" >
    <td><?=$no?> </td>
    <td><?= $row['sno'] ?> </td> 
    <td><?= $row['sname'] ?> </td>
    <td><?= $row['kor'] ?> </td>
    <td><?= $row['eng'] ?> </td>
    <td><?= $row['math'] ?> </td>
    <td><?= $row['hist'] ?> </td> 
    <td><?= $sum?> </td> 
    <td><?= $avg?> </td>
    <td><?= $grade?> </td> 
 </tr>
<?
}
?>
<tr>
    <td colspan=3> &nbsp;누적합</td> 
    <td><?= $ksum?> </td>
    <td><?= $esum?> </td>
    <td><?= $msum?> </td>
    <td><?= $hsum?> </td>
    <td><?= $ssum?> </td> 
    <td><?= $asum?> </td>
    <td> </td>
 </tr>

 <tr>
    <td colspan=3> &nbsp;누적평균(<?=$count?>명)</td> 
    <td><?= $kavg?> </td>
    <td><?= $eavg?> </td>
    <td><?= $mavg?> </td>
    <td><?= $havg?> </td>
    <td><?= $savg?> </td> 
    <td><?= $aavg?> </td>
    <td> </td>
 </tr>
</table>
</div>
<br>
</section>

<? include 'bottom.php'?>

 

 

form.php 파일 


<? include 'top.php'?>
<? include 'DBconn.php'?>

<style>

    table{                 //style 내용 추가
         width:300px;
         height:300px;
    }
    
tr {
 text-align:center;       // 테이블 가운데 정렬하기 
    }
    
</style>

<section>
<br>
<div align=center>
<h2> 학생 성적 입력 </h2>

<table border=1 >    //테이블 만들기 
  <tr> <td>학&nbsp;번</td><td><input type=text name=sno></td> </tr>   //&nbsp; 한칸 띄기
  <tr> <td>성&nbsp;명</td><td><input type=text name=sname></td> </tr>
  <tr> <td>국&nbsp;어</td><td><input type=text name=kor></td> </tr>
  <tr> <td>영&nbsp;어</td><td><input type=text name=eng></td> </tr>
  <tr> <td>수&nbsp;학</td><td><input type=text name=math></td> </tr>
  <tr> <td>역&nbsp;사</td><td><input type=text name=hist></td> </tr>
  <tr> <td colspan=2 align=center><input type=submit value="성적저장"> </td></tr>
</table>

</div>
<br>
</section>

<? include 'bottom.php'?>

 

top.php 파일


<link href="top.css" rel="stylesheet"  type="text/css"  // css 내용 추가 
charset="UTF-8" />



<header> 학생 성적처리 Ver 11. </header>
<nav>
 &emsp;&emsp;&emsp;<a href=form.php> 성적입력 </a> 
 &emsp;<a href=list.php> 성적조회 </a>
 &emsp;<a href=index.php> 홈으로 </a>
</nav>

 

top.css 파일 만들어서 내용 넣기 


header{
    background-color:#ffb6c1;
    height:50px;
    line-height:50px;
    font-size:20px; color: #ffffff ;
    text-align: center ;

}

nav{
    background-color:#fffacd;
    height:30px;
    line-height:30px;
    font-size:13px; color: #ffffff ;

}

section{
    background-color:#ffe4e1;
    font-size:12px;
    min-height: 500px;

}

footer{
    background-color:#ffb6c1;
    height:40px;
    line-height:20px;
    font-size:14px; color: #ffffff ;
    text-align: center ;

}

table{
    width: 500px;

}

성적입력

 

<form action=form_ok.php>
<table border=1 >
  <tr> <td>&nbsp;</td><td><input type=text name=sno></td> </tr>
  <tr> <td>&nbsp;</td><td><input type=text name=sname></td> </tr>
  <tr> <td>&nbsp;</td><td><input type=text name=kor></td> </tr>
  <tr> <td>&nbsp;</td><td><input type=text name=eng></td> </tr>
  <tr> <td>&nbsp;</td><td><input type=text name=math></td> </tr>
  <tr> <td>&nbsp;</td><td><input type=text name=hist></td> </tr>
  <tr> <td colspan=2 align=center>
    <input type=submit value="성적저장"> </td></tr>
</table>
</form>
 
테이블을 form으로 감싸줌
 

주소창에 있는 형태로 값이 넘어감 

 

form_ok.php  파일 만들기 


<? include 'DBconn.php'?>
<?
$sno =$_GET['sno'];  //대소문자 구분해서 써줘야함 
$sname=$_GET['sname'];
$kor=$_GET['kor'];
$eng=$_GET['eng'];
$math=$_GET['math'];
$hist=$_GET['hist'];

    $SQL= "insert into exambl(sno, sname, kor, eng, math, hist )";
    $SQL= $SQL . "values ('$sno', '$sname', '$kor', '$eng', '$math', '$hist')";
$conn->query($SQL); //실행시키기 
?>

<script>
    location.href="list.php";     //이 내용 써주면 성적입력창에서 입력확인버튼 누르면 
</script>                         //바로 성적테이블표 화면으로 넘어감

 

list.php 파일에서 

$avg = round($sum / 4, 1) ;
 $kavg = round($ksum / $count, 1);
    $eavg = round($esum / $count, 1);
    $mavg = round($msum / $count, 1);
    $havg = round($hsum / $count, 1);
    $savg = round($ssum / $count, 1);
    $aavg = round($asum / $count, 1);

수정해주기 , 소수점 한자리수 까지 나타내게 하는 함수 

성적 입력 해주면 

성적조회 테이블에 추가 되어 들어간다 

 

평균 클릭하면 삭제될 수 있게 만들어보기 

 

list 파일에 

<td><?= $avg?> </td> 를

<td> <a href=del.php?sno=<?= $row['sno'] ?>><?= $avg?> </a></td>
로 수정해주기 

 

링크가 생기고 링크 위에 마우스 가져면 왼쪽 아래 정보 보임 

 

del.php  파일 만들어주고 

<? include 'DBconn.php'?>
<?
$sno =$_GET['sno']; 


$SQL= "delete from examtbl where sno = '$sno' ";
$conn -> query($SQL);
?>

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

 

링크 눌러주면 행 삭제가 됨 

 

내용 수정해보기 

list 파일에

 <td>
    <a href="edit.php?sno=<?= $row['sno'] ?>">
    <?= $row['sno'] ?>
</a>
</td> 수정해주고 
edit.php 만들기 




<? include 'top.php'?>
<? include 'DBconn.php'?>
<?
 $sno =$_GET['sno'];
 $SQL = "select sno,sname,kor,eng,math,hist from examtbl where sno= '$sno'" ; 

 $result=$conn -> query($SQL);
 $row = $result -> fetch_assoc() ;

 $sno=$row['sno'];
 $sname=$row['sname'];
 $kor=$row['kor'];
 $eng=$row['eng'];
 $math=$row['math'];
 $hist=$row['hist'];

 ?>

<style>

    table{
         width:300px;
         height:300px;
    }

tr {
 text-align:center;
    }

</style>

<section>
<br>
<div align=center>
<h2> 학생 성적 수정 </h2>

<form action=update_ok.php>
<table border=1 >
  <tr> <td>학&nbsp;번</td><td><input type=text name=sno value="<?=$sno?>"></td> </tr>
  <tr> <td>성&nbsp;명</td><td><input type=text name=sname value="<?=$sname?>"></td> </tr>
  <tr> <td>국&nbsp;어</td><td><input type=text name=kor value="<?=$kor?>"></td> </tr>
  <tr> <td>영&nbsp;어</td><td><input type=text name=eng value="<?=$eng?>"></td> </tr>
  <tr> <td>수&nbsp;학</td><td><input type=text name=math value="<?=$math?>"></td> </tr>
  <tr> <td>역&nbsp;사</td><td><input type=text name=hist value="<?=$hist?>"></td> </tr>
  <tr> <td colspan=2 align=center>
    <input type=submit value="성적저장"> </td></tr>
</table>
</form>

</div>
<br>
</section>

<? include 'bottom.php'?>

 

update_ok.php 파일 만들기 



<? include 'DBconn.php'?>
<?
$sno =$_GET['sno']; 
$sname=$_GET['sname'];
$kor=$_GET['kor'];
$eng=$_GET['eng'];
$math=$_GET['math'];
$hist=$_GET['hist'];

$SQL= "update examtbl set sname='$sname', ";
$SQL= $SQL . "kor = '$kor' , ";
$SQL= $SQL . "eng = '$eng' , ";
$SQL= $SQL . "math = '$math' , ";
$SQL= $SQL . "hist = '$hist' where sno = '$sno' ";

//echo $SQL;
$conn -> query($SQL);
?>

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

 

수정하고싶은 학생 학번 링크를 클릭하면 

학생 성적 수정하는 창이 뜨고 

값을 입력한뒤에 성적 저장을 누르면 수정완료된다 

 

 

index.php 에서 테이블 만들기 연습

-> 카페에 올리기