관리 메뉴

웹개발자의 기지개

[PHP] 실무예제, 동적 테이블 칼럼 값처리하기 본문

PHP

[PHP] 실무예제, 동적 테이블 칼럼 값처리하기

http://portfolio.wonpaper.net 2021. 7. 7. 04:22

상기 이미지처럼 가로방향으로 칼럼열이 동적으로 변동될수 있고, 세로 row행으로 추가로 계속 데이터를 넣을 수 있게 만든다고 생각해보자.

물론 동그라미는 관리자화면에서 체크박스형태로 체크하면 위 그림처럼 나오도록 하면된다.

 

여러가지 방법론이 많겠지만, 필자 나름대로 고안하고 소스를 정리해 보았다.

 

일단 두가지 테이블을 만들었다.

[ materials ] - 실제 데이터 테이블 

 

[ materials_title ] - 가로의 동적인 칼럼 테이블 (최대 15개 칼럼까지 )

 

전체 목록 기본페이지
타이틀 칼럼 정보 변경 페이지
데이터 등록페이지

 

[ materials.php ] - 기본 목록 관리페이지

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?
include "../inc/config.php";
 
$query = "select * from materials_title ";
$tRes  = mysql_query($query) or die("칼럼 타이틀 정보 얻기 실패");
$tRow  = mysql_fetch_array($tRes);
 
$query = "select * from materials";
$res  = mysql_query($query) or die("정보 얻기 실패");
 
?>
 
<form name="f1" method="post">
 
        <table class="table_01">
        <colgroup>
            <col width="8%"  />
            <col width="*"  />
 
<?
$real_num = 0;
for ($i=1;$i<=15;$i++) {
    if ($tRow["c".$i]) {
?>
                        <col width="100"  />
 
<?
        $real_num++;    // 실제 칼럼수
    }
}
?>
            <col width="120"  />
        </colgroup>
        <thead>
        <tr>
            <th>번호</th>
            <th>제품</th>
 
    <?
    for ($i=1;$i<=15;$i++) {
        if ($tRow["c".$i]) {
    ?>
                <th><?=$tRow["c".$i]?></th>
    <?
        }
    }
    ?>
 
            <th>관리</th>
        </tr>
        </thead>
        <tbody>
<?
$num =1 ;
while ($row=mysql_fetch_array($res)) {
?>
        <tr>
            <td style="text-align:center;"><?=$num?></td>
            <td style="text-indent:10px;"><?=stripslashes($row['product_name'])?></td>
 
    <?
        for ($i=1;$i<=$real_num;$i++) {
    ?>
            <td style="text-align:center;" ><input type="checkbox" id="c<?=$i?>_<?=$row['no']?>" name="c<?=$i?>_<?=$row['no']?>" value="y" <?if ($row['c' . $i]=="y"echo "checked";?>></td>            
    <?
        }
    ?>
            <td style="text-align:center;"><input type="button" value="변경" onclick="mMod('<?=$row[no]?>','<?=$real_num?>')"> <input type="button" value="삭제" onclick="mDel('<?=$row[no]?>')"></td>
        </tr>
<?
    $num++;
}
 
if ($num==1) {
?>
        <tr>
            <td colspan="15" style="text-align:center;">등록된 정보가 없습니다.</td>
        </tr>
<?
}
?>
 
        </tbody>
        </table>
 
</form>
 
<script>
function mDel(no) {
    if (confirm("정말로 삭제하시겠습니까?"))
    {
        location.href="materials_del.php?no=" + no;
    }
}
 
function mMod(no, cnt) {
    var str = "";
    var chk = "";
    cnt = parseInt(cnt);
    for (i=1;i<=cnt ;i++ )
    {
        //chk = document.getElementById("c" + i + "_" + no).value;
        if (document.getElementById("c" + i + "_" + no).checked)
        {
            chk = "y";
        } else {
            chk = "n";
        }
 
        if (str=="")
        {
            str = chk;
        } else {
            str += "|" + chk;
        }
    }
 
    location.href="materials_mod.php?no=" + no + "&str=" + str;
}
</script>
 
cs

 

[ materials_reg.php ] - 데이터 등록

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
<form name="f1" method="post" enctype="multipart/form-data">
<table width="100%" class="board_table_write">
<tbody>
<tr>
    <th>재료명</th>
    <td class="txt_left">
        <input type="text" name="product_name" value="" style="width:500px;"/>
    </td>
</tr>
 
 
</tbody>
</table>
 
</form>
 
        <div class="btns_layout">
            <a href="javascript:regChk();" class="color_btn">등록</a> 
            <a href="materials.php" class="bbs_gray_btn">취소</a>
        </div>        
 
<script>
function regChk() {
    var form = document.f1;
    if (form.product_name.value=="")
    {    
        alert("재료명을 입력해 주십시오.");
        form.product_name.focus();
        return;
    }
 
    form.action = "materials_reg_ok.php";
    form.submit();
}
</script>
 
cs

 

[ materials_reg_ok.php ]

 

1
2
3
4
5
6
7
8
9
10
11
12
<?
$product_name     = trim($_POST["product_name"]);
$product_name     = addslashes($product_name);
 
 
$query = "insert into materials (product_name) values ('$product_name')";
mysql_query($query) or die("정보 삽입 실패");
 
?><script>
alert("해당 재료가 새롭게 등록되었습니다.");
location.href="materials.php";
</script>
cs

 

[ materials_del.php ] - 데이터 한줄 삭제

 

1
2
3
4
5
6
7
8
9
10
<?
$no     = trim($_GET["no"]);
 
$query = "delete from materials where no=$no";
mysql_query($query) or die("정보 삽입 실패");
 
?><script>
alert("해당 재료가 삭제되었습니다.");
location.href="materials.php";
</script>  
cs

 

[ materials_mod.php ] - 데이터 한줄 체크박스 정보 변경 ( 칼럼 동적 처리)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?
$no     = trim($_GET["no"]);
$str = trim($_GET["str"]);
 
 
$strArr = explode('|',$str);
 
$gap = "";
for ($i=0;$i<sizeof($strArr);$i++) {
    if (!$gap) {
        if ($strArr[$i]=="y") {
            $gap = "c" . ($i+1) . "='y'";
        } else {
            $gap = "c" . ($i+1) . "=''";
        }
        //echo $gap . " / <br>";
    } else {
        if ($strArr[$i]=="y") {
            $gap .= ",c" . ($i+1) . "='y'";
        } else {
            $gap .= ",c" . ($i+1) . "=''";
        }
        //echo $gap . " / <br>";
    }
 
}
 
$query = "update materials Set $gap where no=$no";
mysql_query($query) or die("정보 삽입 실패");
 
?><script>
alert("해당 재료가 수정되었습니다.");
location.href="materials.php";
</script>  
cs

 

[ materials_title.php ] - 데이터 칼럼 정보 변경 틀페이지

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?
$query = "select * from materials_title ";
$tRes  = mysql_query($query) or die("칼럼 타이틀 정보 얻기 실패");
$tRow  = mysql_fetch_array($tRes);
 
?>
<form name="f1" method="post">
 
        <table class="table_01">
        <colgroup>
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
            <col width="50"  />
 
        </colgroup>
        <thead>
        <tr>
            <th>칼럼1</th>
            <th>칼럼2</th>
            <th>칼럼3</th>
            <th>칼럼4</th>
            <th>칼럼5</th>
            <th>칼럼6</th>
            <th>칼럼7</th>
            <th>칼럼8</th>
            <th>칼럼9</th>
            <th>칼럼10</th>
            <th>칼럼11</th>
            <th>칼럼12</th>
            <th>칼럼13</th>
            <th>칼럼14</th>
            <th>칼럼15</th>
        </tr>
        </thead>
        <tbody>
 
        <tr>
            <td style="text-align:center;"><input type="text" id="c1" name="c1" value="<?=stripslashes($tRow['c1'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c2" name="c2" value="<?=stripslashes($tRow['c2'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c3" name="c3" value="<?=stripslashes($tRow['c3'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c4" name="c4" value="<?=stripslashes($tRow['c4'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c5" name="c5" value="<?=stripslashes($tRow['c5'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c6" name="c6" value="<?=stripslashes($tRow['c6'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c7" name="c7" value="<?=stripslashes($tRow['c7'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c8" name="c8" value="<?=stripslashes($tRow['c8'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c9" name="c9" value="<?=stripslashes($tRow['c9'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c10" name="c10" value="<?=stripslashes($tRow['c10'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c11" name="c11" value="<?=stripslashes($tRow['c11'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c12" name="c12" value="<?=stripslashes($tRow['c12'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c13" name="c13" value="<?=stripslashes($tRow['c13'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c14" name="c14" value="<?=stripslashes($tRow['c14'])?>" style="width:50px;"></td>
            <td style="text-align:center;"><input type="text" id="c15" name="c15" value="<?=stripslashes($tRow['c15'])?>" style="width:50px;"></td>
 
        </tr>
 
        </tbody>
        </table>
 
</form>
 
 
        <div class="btns_layout">
        <span style='color:red;'>(*) 해당 타이틀을 삭제하실려면, 글내용을 빈공란으로 지우시고, 변경하기 버튼을 눌러주세요.</span>
        <br><br>
            <a href="javascript:regChk();" class="color_btn">변경하기</a> 
        </div>        
 
<script>
function regChk() {
    if (confirm("해당 정보로 타이틀 내용을 변경합니다."))
    {
        document.f1.action= "materials_title_ok.php";
        document.f1.submit();
    }
}
</script>
cs

 

[ materials_title_ok.php ]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?
$c1             = trim($_POST["c1"]);
$c1     = addslashes($c1);
 
$c2             = trim($_POST["c2"]);
$c2     = addslashes($c2);
$c3             = trim($_POST["c3"]);
$c3  = addslashes($c3);
$c4             = trim($_POST["c4"]);
$c4     = addslashes($c4);
$c5             = trim($_POST["c5"]);
$c5     = addslashes($c5);
$c6             = trim($_POST["c6"]);
$c6     = addslashes($c6);
$c7             = trim($_POST["c7"]);
$c7     = addslashes($c7);
$c8             = trim($_POST["c8"]);
$c8     = addslashes($c8);
$c9             = trim($_POST["c9"]);
$c9     = addslashes($c9);
$c10             = trim($_POST["c10"]);
$c10     = addslashes($c10);
$c11             = trim($_POST["c11"]);
$c11     = addslashes($c11);
$c12             = trim($_POST["c12"]);
$c12  = addslashes($c12);
$c13             = trim($_POST["c13"]);
$c13     = addslashes($c13);
$c14             = trim($_POST["c14"]);
$c14     = addslashes($c14);
$c15             = trim($_POST["c15"]);
$c16     = addslashes($c15);
 
 
$query = "update materials_title Set c1='$c1',c2='$c2',c3='$c3',c4='$c4',c5='$c5',c6='$c6',c7='$c7',c8='$c8',c9='$c9',c10='$c10',c11='$c11',c12='$c12',c13='$c13',c14='$c14',c15='$c15' ";
mysql_query($query) or die("정보 삽입 실패");
 
?><script>
alert("해당 타이틀 정보가 새롭게 변경되었습니다.");
location.href="materials.php";
</script>
cs

 

 

 

Comments