<?php //url 로 직접 불러오는것을 방지 if(!strstr($_SERVER['HTTP_REFERER'],$_SERVER['SERVER_NAME'])&& !strstr($_SERVER['HTTP_REFERER'],'out_excel.php')) exit; $servername = "localhost"; $username = "user ID"; $password = "DB_password"; $table = "xe_documents"; $database = "DB name"; $module_srl = "module srl"; // 숫자 입력 $con = new mysqli($servername,$username,$password,$database); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $getcolumns = array("title"); //출력원하는 칼럼을 배열로 입력 header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=doc_".date('Ymd').".xls"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=Content-Type content=text/html; charset="utf-8"> </head> <body> <table border=1> <tr align=center> <?php //제목 불러오기 $sql = "SHOW COLUMNS FROM $database.$table"; $dbcolumns = mysqli_query($con,$sql); //xe_documents에서 칼럼값 불러오기 // fetch associative array while ($rowname = mysqli_fetch_row($dbcolumns)) { if( in_array($rowname[0], $getcolumns) ) echo "<th>".$rowname[0]."</th>"; } //확장변수의 타이틀은 값이므로 $result = mysqli_query($con, "select var_name from xe_document_extra_keys where module_srl = $module_srl"); while( $data = mysqli_fetch_array($result) ){ echo "<th>".htmlspecialchars($data[var_name])."</th>"; } echo"</tr>"; //데이터 불러오기 $result = mysqli_query($con, "select * from $table where module_srl=$module_srl"); while( $data = mysqli_fetch_assoc($result) ){ echo"<tr>"; foreach( $data as $key => $val ){ if( in_array($key, $getcolumns) ){ if ($key == 'regdate'){ echo "<td style='mso-number-format:\"0_ \";' >".htmlspecialchars($val)."</td>"; }elseif ($key == 'content'){ //내용 출력할경우 태그가 그대로 출력됨 //echo "<td>".$val."</td>"; //셀이 두칸이상으로 늘어남 echo "<td >".htmlspecialchars(strip_tags(str_replace(" ","",$val)))."</td>"; }else { echo "<td>".htmlspecialchars($val)."</td>"; } } if ($key == 'document_srl'){ //확장변수 불러올때 사용 $d_srl=$val; } } //확장변수 출력 //////////////////////////////////////////// //eid리스트를 불러온다 $query=mysqli_query($con, "select eid from xe_document_extra_keys where module_srl = $module_srl"); while($extra_title=mysqli_fetch_array($query)){ echo"<td>"; $egetFields = array("value","eid"); //확장변수 값을 불러와 eid값이 같은곳에 출력 $extra_result = mysqli_query($con, "select * from xe_document_extra_vars where document_srl = $d_srl"); while( $edata = mysqli_fetch_assoc($extra_result) ){ $extra_str=""; foreach( $edata as $key => $val ){ if( in_array($key, $egetFields) ){ $extra_str = $val.",".$extra_str; } } $extra_arr=explode(",",$extra_str); if ($extra_title[eid]==$extra_arr[0]){ //전화번호값은 "|@|" 를 "-" 로 변경 if($extra_title[eid]=="mb_phone_Number"){ echo str_replace("|@|","-",htmlspecialchars($extra_arr[1])); //기타 배열값들은 ',' 로 변경 }elseif($extra_title[eid]=="mb_incomplete"||$extra_title[eid]=="mb_cs"){ echo str_replace("|@|",",",htmlspecialchars($extra_arr[1])); }else{ echo htmlspecialchars($extra_arr[1]); } } } echo"</td>"; //eid값이 없으면 빈칸입력 } ///////////////////////////////////////////////////////////////////// echo"</tr>"; } mysqli_close($con); ?> </table> </body> </html>