기록해! 정리해!

HTML --성적조회 프로그램Ver 1.0 (3 , 빅데이터) 본문

Visual studio (PHP)

HTML --성적조회 프로그램Ver 1.0 (3 , 빅데이터)

zsuling 2022. 6. 22. 10:51

06/22

 

º 폴더 정리

member ~ .php  -> 새폴더 member

join_list -> 새폴더 join

delete, edit, form, form_ok, list, update_ok -> 새폴더 jungbo

top, bottom, DBconn -> 새폴더 include

 

º 127.0.0.1에 연결하기 (PHP 경로설정)

:로컬주소(절대경로)로 연결해준다

 

링크는 웹사이트로 넘어가는 거라 사이트의 절대경로를 잡는 것

include는 하드디스크의 로컬주소를 가지고 경로를 잡는 것 ( path를 잡아줘야함 )

 

top.php에 추가하기

<?
$host=$_SERVER['HTTP_HOST'];
$path="http://".$host;
// echo $path;
// echo "<br>" ; 
// echo $_SERVER["DOCUMENT_ROOT"];
--로컬의 절대경로 ( 내 컴퓨터의 C드라이브)
?>

 

모든 include에  $_SERVER["DOCUMENT_ROOT"]. 추가

<? include $_SERVER["DOCUMENT_ROOT"]."/include/top.php"?>

 

링크에 <?=$path?> 추가

<a href='<?=$path?>/jungbo/form.php'>성적입력</a>

 

이미지에는 . 대신 <?=$path?> 추가

<tr><td colspan=2 align=center width=400 height=150><img src=<?=$path?>/img/<?=$img?>></td></tr>

move_uploaded_file($tmp, "$path/img/$mfile");

 

º  자료실 복습

1) 자동증가

오토셋에 school 테이블 생성

 

Create table school (

idx int not null auto_increment primary key,

name varchar(10) not null,

file varchar(50) not null,

etc varchar(200)

);

 

 

insert into school (name, file, etc) values ('young','young.jpg');

insert into school (idx, name, file, etc) values ('101', 'young','young.jpg');

insert into school (name, file, etc) values ('young','young.jpg');

 

자동으로 idx가 증가함

레코드 3개 다 지우고

insert into school (name, file, etc) values ('young','young.jpg'); 하면

idx가 103으로 잡힘

 

2) 비주얼코드에 새폴더 school 만들고 새파일 form.php 생성

<? include $_SERVER["DOCUMENT_ROOT"]."/include/top.php" ?>

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


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

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

<section> 
<br><br>
<div id=section1>   
 학생 정보 수집
</div>
<br>
<div align=center>
<form name=f1 action="form_ok.php"  onSubmit="return ck1()"
      method="post"
      enctype="multipart/form-data">
<table border=1>
<tr><td width=100 class="td1">이름</td>
<td>&nbsp;<input  type=text  name=name> </td></tr>

<tr><td  class="td1">특이<br>사항</td><td>
    <textarea cols=40 rows=5 name=etc> </textarea>
  </td></tr>

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

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

<? include $_SERVER["DOCUMENT_ROOT"]."/include/bottom.php" ?>

3) 내컴퓨터에 school안에 새폴더 file 생성 후 속성에서 everyone 추가하고

    비주얼코드에서 school안에 새파일 form_ok.php 생성

 

4) form_ok.php

 

if($mfile == ""){ --파일을 안넣었을 때
    $mfile == "space.png"; 
}else{

 

if(file_exists("./files/$mfile")){ --중복된 파일이면

     $f_fname = basename($mfile,strrchr($mfile,'.'));  
     $time = date("His", time());
     $ext = strrchr($mfile,'.') ;
     $mfile = $f_fname."_".$time.$ext ;

     move_uploaded_file($tmp, "./files/$mfile");
     
}else{ --중복파일 아니면
    move_uploaded_file($tmp, "./files/$mfile");
}

 

º  PHP 전역변수/지역변수

: 전역변수를 선언하면 그 괄호안 말고 바깥에서든지 아무데서나 출력할 수 있다

 

<?php
$name = $_REQUEST['name'];
$etc = $_POST['etc'];
$mfile = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];

if($mfile == ""){
    $mfile == "space.png";
}else{

if(file_exists("./files/$mfile")){

     $f_fname = basename($mfile,strrchr($mfile,'.'));
     $time = date("His", time());
     $ext = strrchr($mfile,'.') ;
     $mfile = $f_fname."_".$time.$ext ;

     move_uploaded_file($tmp, "./files/$mfile");

}else{
    move_uploaded_file($tmp, "./files/$mfile");
}

}

echo $mfile ;
?>

 

form_ok.php

<? include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ?>

<?
$name = $_REQUEST['name'];
$etc = $_POST['etc'];
$mfile = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];

if($mfile == ""){
    $mfile == "space.png";
}else{

if(file_exists("./files/$mfile")){

     $f_fname = basename($mfile,strrchr($mfile,'.'));
     $time = date("His", time());
     $ext = strrchr($mfile,'.') ;
     $mfile = $f_fname."_".$time.$ext ;

     move_uploaded_file($tmp, "./files/$mfile");

}else{
    move_uploaded_file($tmp, "./files/$mfile");
}

}

$SQL = "insert into school (name, etc, file) values ('$name', '$etc', '$mfile')";
$conn -> query( $SQL);
?>

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

4)  form_list.php 학생 목록 조회

  &emsp;<a href='<?=$path?>/school/form_list.php'>학생목록</a> 
 

º  검색창 만들기

form_list.php에

 

<?

$ch1 = $_GET['ch1'];
$ch2 = $_GET['ch2'];

 $SQL = "select * from  school " ; 
 $SQLEnd = " order by idx desc ";

if( $ch1 == "" ){
    $SQL = $SQL . $SQLEnd ;
    
}else if( $ch1 == "idx"){
    $SQL = $SQL . " where idx like '%$ch2%' " . $SQLEnd ;
    
}else if( $ch1 == "name"){
    $SQL = $SQL . " where name like '%$ch2%' " . $SQLEnd ;
}
$result = $conn -> query($SQL);

?>

.

.

.

<form >
        <select name=ch1>
            <option value="idx">순번</option>
            <option value="name">이름</option>
        </select>
        <input type=text name=ch2>
        <input type=submit value="검색하기">
</form>

 

º  사진에 링크걸고 삭제하기

form_list.php에

<td><a href=form_delete.php?idx=<?=$row['idx']?>>
     <img src =./files/<?=$row['file']?> width=30 height=30 >  </a></td>

<? include $_SERVER["DOCUMENT_ROOT"]."/include/top.php" ?>
<? include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ?>
<?

$ch1 = $_GET['ch1'];
$ch2 = $_GET['ch2'];

 $SQL = "select * from  school " ; 
 $SQLEnd = " order by idx desc ";

if( $ch1 == "" ){
    $SQL = $SQL . $SQLEnd ;
    
}else if( $ch1 == "idx"){
    $SQL = $SQL . " where idx like '%$ch2%' " . $SQLEnd ;
    
}else if( $ch1 == "name"){
    $SQL = $SQL . " where name like '%$ch2%' " . $SQLEnd ;
}
$result = $conn -> query($SQL);

?>


<style>
 table{
   width: 650px ;
 } 

 td{
   text-align:center; 
   height: 25px ;
 }
</style>
<section> 
<br>
<div id=section1>   
 학생 목록 조회
</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['idx']?></td>     
     <td><?=$row['name']?></td>
     <td><?=$row['file']?> </td>     
     <td><a href=form_delete.php?idx=<?=$row['idx']?>>
     <img src =./files/<?=$row['file']?> width=30 height=30 >  </a></td>
</tr>
<? }  ?>

</table>
<form >
        <select name=ch1>
            <option value="idx">순번</option>
            <option value="name">이름</option>
        </select>
        <input type=text name=ch2>
        <input type=submit value="검색하기">
</form>

<br>
</div>                   
</section>

<? include $_SERVER["DOCUMENT_ROOT"]."/include/bottom.php" ?>

form_delete.php

<? include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ?>
<?
  $idx = $_GET['idx'];
 
  $SQL = "select * from school where idx='$idx'";
  $result = $conn -> query($SQL);
  $row = $result -> fetch_assoc();
  $file = $row['file'];

  if ($row['file'] != "space.png" ){
   unlink("./files/$file");
}
 
$SQL ="delete from school where idx ='$idx' " ;
$conn -> query($SQL);

?>
<script>
 location.href="form_list.php" ;
</script>

º 빅데이터 만들기

새폴더 bigdata 만들고 그 안에 insert.php 와 list.php 생성

 

오토셋에 board 추가하기

Create table board {

idx int not null auto_increment primary key,

name varchar(10) not null,

age int not null,

title varchar

 

insert.php에

 

include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ;

//난수발생기
$randomName = mt_rand(0, 9);
$randomAge = mt_rand(0, 3);
$randomtitle1 = mt_rand(0, 4);
$randomtitle2 = mt_rand(0, 4);
$randometc1 = mt_rand(0, 4);
$randometc2 = mt_rand(0, 4);
$randometc3 = mt_rand(0, 4);

$name = array('하니','똘이','영심이','만수','지효','둘리','하나','둘','셋','넷');
$age = array('12','13','14','15');
$title1 = array('ASP','JSP','PHP','JAVA','C#');
$title2 = array('초급','중급','고급','심화','실무');

$etc = array('산','바다','강','하늘','땅');

//배열적용
$name=$name[$randomName];
$age=$age[$randomAge];
$title=$title1[$randomtitle1]." , ".$title2[$randomtitle2];
$etc= $etc[$randometc1]." , ".$etc[$randometc2]." , ".$etc[$randometc3] ;

$SQL = "insert into board (name, age, title, etc) values ('$name','$age','$title','$etc' )";

//echo $SQL;

$conn -> query($SQL);
$conn -> close(); -- 커넥션 닫기 (많은 숫자를 열면 서버가 다운되기 때문에 닫아줍니다..)

 

하고  for ($i=1 ; $i <= 53 ; $i++ ) 문으로 루프돌리기

 

insert.php 

<?
for ($i=1 ; $i <= 53 ; $i++ ){
 include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ;

//난수발생기
$randomName = mt_rand(0, 9);
$randomAge = mt_rand(0, 3);
$randomtitle1 = mt_rand(0, 4);
$randomtitle2 = mt_rand(0, 4);
$randometc1 = mt_rand(0, 4);
$randometc2 = mt_rand(0, 4);
$randometc3 = mt_rand(0, 4);

$name = array('하니','똘이','영심이','만수','지효','둘리','하나','둘','셋','넷');
$age = array('12','13','14','15');
$title1 = array('ASP','JSP','PHP','JAVA','C#');
$title2 = array('초급','중급','고급','심화','실무');

$etc = array('산','바다','강','하늘','땅');

//배열적용
$name=$name[$randomName];
$age=$age[$randomAge];
$title=$title1[$randomtitle1]." , ".$title2[$randomtitle2];
$etc= $etc[$randometc1]." , ".$etc[$randometc2]." , ".$etc[$randometc3] ;



$SQL = "insert into board (name, age, title, etc) values ('$name','$age','$title','$etc' )";

//echo $SQL;

$conn -> query($SQL);
$conn -> close();

}

?>

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

 list.php

<? include $_SERVER["DOCUMENT_ROOT"]."/include/top.php"?>
<? include $_SERVER["DOCUMENT_ROOT"]."/include/DBconn.php" ?>
<?

$SQL = " select * from board " ;

$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>
 </tr>

<? 

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


<tr>
    <td><?=$row['idx']?></td> 
    <td><?=$row['name']?></td> 
    <td><?=$row['age']?></td> 
    <td><?=$row['title']?></td>
    <td><?=$row['etc']?></td> 
</tr>
<?}?>

</table>
<br>
</div>
</section>
<? include $_SERVER["DOCUMENT_ROOT"]."/include/bottom.php"?>

순번이 0으로 끝나지않게끔만 계속 생성해주자

 

-- 상위 10개만 출력하기

$SQL = " select * from board order by idx asc limit 0, 10 " ;

 

-- 이전, 다음 만들기

 

링크부터 걸기

<a href=list.php?idx=<?=$idx-10?>> 이전 </a> &emsp;
<a href=list.php?idx=<?=$idx+10?>> 다음 </a>

 

윗 부분에 

<?
    if($_GET['idx']==""){
        $idx=0;
        }
    else{
        $idx=$_GET['idx'];
    }

    $SQL = "select * from board list order by idx asc limit $idx, 10";

    $result = $conn -> query($SQL);
?>

 

--첫 페이지는 이전으로 안넘어가게 하기

<? if($idx == 0){ ?>
     이전 &emsp;
<?}else{?>
    <a href=list.php?idx=<?=$idx-10?>> 이전 </a> &emsp;
<?}?>
    <a href=list.php?idx=<?=$idx+10?>> 다음 </a> 

 

--처음으로

 <a href=list.php?idx?idx=0> 처음으로 </a> &emsp;

 

 list.php

<? include $_SERVER["DOCUMENT_ROOT"]."/include/top.php"?>
<? include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ?>

<?
    if($_GET['idx']==""){
        $idx=0;
        }
    else{
        $idx=$_GET['idx'];
    }

    $SQL = "select * from board list order by idx asc limit $idx, 10";

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

?>
<style>
    table{
        width:500px;
    }
    td{
        text-align:center;
        height:25px;
    }

</style>
<section>
    <br>
    <div id=section1>
    <b>빅데이터 목록 조회</b> <br>
    </div>
    <br>
    <div align=center>
    <table border=1>
        <tr>
            <td>순번</td><td>이름</td><td>나이</td>
            <td>제목</td><td>특이사항</td>
        </tr>


    <? while( $row = $result -> fetch_assoc() ){  
                 
     
            $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_ssum=$s_ssum+$row['ssum'];
            $s_savg=$s_savg+$row['savg'];
       
          
     ?>
        <tr>
            <td><?=$row['idx']?></td>
            <td><?=$row['name']?></td>
            <td><?=$row['age']?></td>
            <td><?=$row['title']?></td>
            <td><?=$row['etc']?></td>
        </tr>
    <? }   ?>

    </table>
    
    <a href=list.php?idx?idx=0> 처음으로 </a> &emsp;

<? if($idx == 0){ ?>
     이전 &emsp;
<?}else{?>
    <a href=list.php?idx=<?=$idx-10?>> 이전 </a> &emsp;
<?}?>
    <a href=list.php?idx=<?=$idx+10?>> 다음 </a> 
    
    <br>
    </div>
</section>


<? include $_SERVER["DOCUMENT_ROOT"]."/include/bottom.php"?>

 

-- 레코드의 카운트 세기

    $SQL_tc = "select count(*) tc from board ";
    $result_tc = $conn -> query($SQL_tc);
    $row_tc = $result_tc -> fetch_assoc() ;

.

.

  전체 레코드 수 : <?=$row_tc['tc']?>

--전체,현재 페이지수 세기

  $total_page = ceil($row_tc['tc'] / 10) ;

  $now_page = ($idx / 10) +1 ;

.

.

.

 페이지 수 : <?=$total_page?>

 현재 페이지 : <?=$now_page?> 

-- 마지막 페이지에 다음 링크 빼기

<? if($now_page == $total_page ){ ?>
     다음 
    <? } else { ?> 
    <a href=list.php?idx=<?=$idx+10?>> 다음 </a> 
    <? } ?>

 

--마지막으로 만들기

 <? $end_page = ($total_page - 1) * 10 ; ?>
    <a href=list.php?idx=<?=$end_page?>> 마지막으로 </a> 

list.php

<? include $_SERVER["DOCUMENT_ROOT"]."/include/top.php"?>
<? include $_SERVER["DOCUMENT_ROOT"]."/include/DBConn.php" ?>

<?
    if($_GET['idx']==""){
        $idx=0;
        }
    else{
        $idx=$_GET['idx'];
    }

    $SQL_tc = "select count(*) tc from board ";
    $result_tc = $conn -> query($SQL_tc);
    $row_tc = $result_tc -> fetch_assoc() ;

    $SQL = "select * from board list order by idx asc limit $idx, 10";
    $result = $conn -> query($SQL);

    $total_page = ceil($row_tc['tc'] / 10) ;
    $now_page = ($idx / 10) +1 ;
?>
<style>
    table{
        width:500px;
    }
    td{
        text-align:center;
        height:25px;
    }

</style>
<section>
    <br>
    <div id=section1>
    <b>빅데이터 목록 조회</b> <br>
    </div>
    <br>
    <div align=center>
        전체 레코드 수 : <?=$row_tc['tc']?> <br>
        페이지 수 : <?=$total_page?>  &emsp; 현재 페이지 : <?=$now_page?> 
    <table border=1>
        <tr>
            <td>순번</td><td>이름</td><td>나이</td>
            <td>제목</td><td>특이사항</td>
        </tr>


    <? while( $row = $result -> fetch_assoc() ){  
                 
     
            $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_ssum=$s_ssum+$row['ssum'];
            $s_savg=$s_savg+$row['savg'];
       
          
     ?>
        <tr>
            <td><?=$row['idx']?></td>
            <td><?=$row['name']?></td>
            <td><?=$row['age']?></td>
            <td><?=$row['title']?></td>
            <td><?=$row['etc']?></td>
        </tr>
    <? }   ?>

    </table>
    
    <a href=list.php?idx?idx=0> 처음으로 </a> &emsp;

<? if($idx == 0){ ?>
     이전 &emsp;
<?}else{?>
    <a href=list.php?idx=<?=$idx-10?>> 이전 </a> &emsp;
<?}?>
<? if($now_page == $total_page ){ ?>
     다음  &emsp;
    <? } else { ?> 
    <a href=list.php?idx=<?=$idx+10?>> 다음 </a>  &emsp;
    <? } ?>

    <? $end_page = ($total_page - 1) * 10 ; ?>
    <a href=list.php?idx=<?=$end_page?>> 마지막으로 </a> 


    <br>
    </div>
</section>


<? include $_SERVER["DOCUMENT_ROOT"]."/include/bottom.php"?>

 

Comments