
https://xe1.xpressengine.com/tip/22795784
위 링크에 있는 내용대로 게시판 목록을 엑셀로 추출하는 php를 테스트 하고 있는데요. 기본 소스로는 카테고리하고 작성자 정보가 제외되서 이걸 포함하여 추출하려고 합니다.
$getFields 변수에 category_srl와 member_srl 를 넣으니 srl넘버가 추출이 되더라고요.
이걸 가지고 카테고리 이름과 username을 따오면 되겠지했는데 XE 함수가 PHP안에서 작동이 안되네요.
($oDocumentModel = getModel('document'); 이런거..)
프로그래밍 언어 보면 import라던가 using같이 외부파일을 선언하는게 php는 없는거 같은데 php파일이 어느 위치에 있냐에 따라 다른건가요? ( 파일은 root에 있습니다만..) 불가능 하다면 그냥 sql query로 다 처리를 해야되는건지요.
혹시 소스에서 간단하게 원하는걸 표시하려면 어떤 명령어로 처리해야되는지 알려주시면 감사하겠습니다.
<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
//url 로 직접 불러오는것을 방지
if(!strstr($_SERVER['HTTP_REFERER'],$_SERVER['SERVER_NAME'])&& !strstr($_SERVER['HTTP_REFERER'],'out_excel.php')) exit;
$info = array(
"host" => "localhost",
"user" => "new---1",
"pass" => "------!",
"db" => "------",
"table" => "xe_documents",
"module_srl" => "406",
);
$getFields = array("category_srl","title","regdate","content","member_srl"); //출력원하는 칼럼을 배열로 입력
header( "Content-type: application/vnd.ms-excel; charset=utf-8" );
header("Content-Disposition: attachment; filename=doc_".date('Ymd').".xls");
//print("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel; charset=euc-kr\">");
$con = mysql_connect($info[host], $info[user], $info[pass]) || die(mysql_error());
mysql_query("set names utf8");
$con_db = mysql_select_db($info[db]) || die(mysql_error());
?>
<!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
//제목 불러오기
$fields = mysql_list_fields("$info[db]", "$info[table]");
$columns = mysql_num_fields($fields);
//echo print_r($columns);
/*$oDocumentModel = getModel('document');
$category_info = $oDocumentModel->getCategory($document->get('category_srl'));
$mycategory = $category_info->title;*/
/*for( $i=0 ; $i<$columns ; $i++ )
{
$field[$i]=mysql_field_name($fields, $i);
if( in_array($field[$i], $getFields) ) echo "<th>".$field[$i]."</th>";
}
*/
echo "<th>"."기능"."</th>";
echo "<th>"."제목"."</th>";
echo "<th>"."내용"."</th>";
echo "<th>"."작성자"."</th>";
echo "<th>"."작성일자"."</th>";
//확장변수의 타이틀은 값이므로
$result = mysql_query("select var_name from xe_document_extra_keys where module_srl = $info[module_srl]");
//$aaa = mysql_fetch_array($result);
//print_r($aaa); //$result print_r 결과 = Resource id #4
while( $data = mysql_fetch_array($result) )
{
echo "<th>".htmlspecialchars($data[var_name])."</th>";
}
echo"</tr>";
//데이터 불러오기
$result = mysql_query("select * from $info[table] where module_srl = $info[module_srl]");
while( $data = mysql_fetch_assoc($result) )
{
echo"<tr>";
foreach( $data as $key => $val )
{
if( in_array($key, $getFields) )
{
if ($key == 'regdate') {
$str_date = htmlspecialchars($val);
$date = date("Y-m-d", strtotime( $str_date ) );
//echo "<td style='mso-number-format:\"0_ \";' >".htmlspecialchars($val)."</td>";
//echo "<td style='mso-number-format:yyyy-mm-dd' >".htmlspecialchars(substr($val,0,7))."</td>";
echo "<td>".$date."</td>";
}elseif ($key == 'content'){ //내용 출력할경우 태그가 그대로 출력됨
//echo "<td>".$val."</td>"; //셀이 두칸이상으로 늘어남
echo "<td >".htmlspecialchars(strip_tags(str_replace(" ","",$val)))."</td>";
}elseif ($key == 'member_srl'){ //내용 출력할경우 태그가 그대로 출력됨
//$member = htmlspecialchars($val);
//$oMemberModel = getModel('member');
//$member_info = $oMemberModel->getMemberInfoByMemberSrl($member);
//$username = $member_info->user_id;
echo "<td>"."멤버".htmlspecialchars($val)."</td>";
}else {
echo "<td>".htmlspecialchars($val)."</td>";
}
}
if ($key == 'document_srl'){ //확장변수 불러올때 사용
$d_srl=$val;
}
}
//확장변수 출력 ////////////////////////////////////////////
//eid리스트를 불러온다
$query=mysql_query("select eid from xe_document_extra_keys where module_srl = $info[module_srl]");
while($extra_title=mysql_fetch_array($query))
{
echo"<td>";
$egetFields = array("value","eid");
//확장변수 값을 불러와 eid값이 같은곳에 출력
$extra_result = mysql_query("select * from xe_document_extra_vars where document_srl = $d_srl");
while( $edata = mysql_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>";
}
?>
</table>
</body>
</html>
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
//url 로 직접 불러오는것을 방지
if(!strstr($_SERVER['HTTP_REFERER'],$_SERVER['SERVER_NAME'])&& !strstr($_SERVER['HTTP_REFERER'],'out_excel.php')) exit;
$info = array(
"host" => "localhost",
"user" => "new---1",
"pass" => "------!",
"db" => "------",
"table" => "xe_documents",
"module_srl" => "406",
);
$getFields = array("category_srl","title","regdate","content","member_srl"); //출력원하는 칼럼을 배열로 입력
header( "Content-type: application/vnd.ms-excel; charset=utf-8" );
header("Content-Disposition: attachment; filename=doc_".date('Ymd').".xls");
//print("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel; charset=euc-kr\">");
$con = mysql_connect($info[host], $info[user], $info[pass]) || die(mysql_error());
mysql_query("set names utf8");
$con_db = mysql_select_db($info[db]) || die(mysql_error());
?>
<!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
//제목 불러오기
$fields = mysql_list_fields("$info[db]", "$info[table]");
$columns = mysql_num_fields($fields);
//echo print_r($columns);
/*$oDocumentModel = getModel('document');
$category_info = $oDocumentModel->getCategory($document->get('category_srl'));
$mycategory = $category_info->title;*/
/*for( $i=0 ; $i<$columns ; $i++ )
{
$field[$i]=mysql_field_name($fields, $i);
if( in_array($field[$i], $getFields) ) echo "<th>".$field[$i]."</th>";
}
*/
echo "<th>"."기능"."</th>";
echo "<th>"."제목"."</th>";
echo "<th>"."내용"."</th>";
echo "<th>"."작성자"."</th>";
echo "<th>"."작성일자"."</th>";
//확장변수의 타이틀은 값이므로
$result = mysql_query("select var_name from xe_document_extra_keys where module_srl = $info[module_srl]");
//$aaa = mysql_fetch_array($result);
//print_r($aaa); //$result print_r 결과 = Resource id #4
while( $data = mysql_fetch_array($result) )
{
echo "<th>".htmlspecialchars($data[var_name])."</th>";
}
echo"</tr>";
//데이터 불러오기
$result = mysql_query("select * from $info[table] where module_srl = $info[module_srl]");
while( $data = mysql_fetch_assoc($result) )
{
echo"<tr>";
foreach( $data as $key => $val )
{
if( in_array($key, $getFields) )
{
if ($key == 'regdate') {
$str_date = htmlspecialchars($val);
$date = date("Y-m-d", strtotime( $str_date ) );
//echo "<td style='mso-number-format:\"0_ \";' >".htmlspecialchars($val)."</td>";
//echo "<td style='mso-number-format:yyyy-mm-dd' >".htmlspecialchars(substr($val,0,7))."</td>";
echo "<td>".$date."</td>";
}elseif ($key == 'content'){ //내용 출력할경우 태그가 그대로 출력됨
//echo "<td>".$val."</td>"; //셀이 두칸이상으로 늘어남
echo "<td >".htmlspecialchars(strip_tags(str_replace(" ","",$val)))."</td>";
}elseif ($key == 'member_srl'){ //내용 출력할경우 태그가 그대로 출력됨
//$member = htmlspecialchars($val);
//$oMemberModel = getModel('member');
//$member_info = $oMemberModel->getMemberInfoByMemberSrl($member);
//$username = $member_info->user_id;
echo "<td>"."멤버".htmlspecialchars($val)."</td>";
}else {
echo "<td>".htmlspecialchars($val)."</td>";
}
}
if ($key == 'document_srl'){ //확장변수 불러올때 사용
$d_srl=$val;
}
}
//확장변수 출력 ////////////////////////////////////////////
//eid리스트를 불러온다
$query=mysql_query("select eid from xe_document_extra_keys where module_srl = $info[module_srl]");
while($extra_title=mysql_fetch_array($query))
{
echo"<td>";
$egetFields = array("value","eid");
//확장변수 값을 불러와 eid값이 같은곳에 출력
$extra_result = mysql_query("select * from xe_document_extra_vars where document_srl = $d_srl");
while( $edata = mysql_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>";
}
?>
</table>
</body>
</html>
댓글 5
include 'XE설치경로/config/config.inc.php';
Context::getInstance()->init();
맨 위에 이렇게 선언해 주시면 XE 함수를 자유롭게 사용할 수 있습니다.
include '/config/config.inc.php';
include './config/config.inc.php';
include 'C:\inetpub\wwwroot\config\config.inc.php';
이 세가지를 다 해봐도 안되던데 제가 어딜 잘못 한건지 모르겠습니다. 해당 php는 "C:\inetpub\wwwroot" 이 경로에 있습니다.
해당 php 파일이 어디에 있는지는 중요하지 않고, XE가 설치되어 있는 경로로 하셔야 합니다.
같은 경로에 설치되어 있다면 상관없겠지만...
아참, 깜빡했네요. 맨 위에 define('__XE__', true); 도 해주세요.
이게 없으면 XE가 그냥 꺼져 버리더군요 ㅠ