Web/PHP

컨텐츠 xml로 변환

aucd29 2013. 9. 26. 21:45
나의 콘텐츠를 xml로 공급해주는 소스입니다.
페이징 및 검색필드설정을 통한 검색도 가능합니다.
공급된 xml을 읽어서 템플릿을 통해 자기마음대로 보여주는 프로그램을 만들었는데
asp입니다.
<?php
$host = ""; //데이터베이스 아이피
$user = ""; //데이터베이스 유저
$passwd = ""; //데이터베이스 비밀번호
$user_db = ""; //데이터베이스 이름
$sqlRS = "select * from 테이블명 where 검색조건 order by 정렬필드"; //리스트를 만들어낼 sql문
$colID = ""; //콘텐츠고유아이디필드명
$colTitle = ""; //콘텐츠제목필드명
$colDate = ""; //콘텐츠작성일필드명
$colWriter = ""; //콘텐츠작성자필드명
$colDescription = ""; //콘텐츠내용필드명
$colDescriptionType = ""; //콘텐츠내용타입필드명
$valDescriptionText = ""; //콘텐츠내용타입이텍스트인경우의값
$colEmail = ""; //콘텐츠작성자이메일필드명
$colHomepage = ""; //콘텐츠작성자홈페이지필드명
$colImage1 = ""; //콘텐츠이미지필드명1

//$colImage2 = ""; //콘텐츠이미지필드명2
//$colOption1 = ""; //콘텐츠옵션1필드명
//$colOption2 = ""; //콘텐츠옵션2필드명
//$colOption3 = ""; //콘텐츠옵션3필드명
//$colOption4 = ""; //콘텐츠옵션4필드명
//$colOption5 = ""; //콘텐츠옵션5필드명

//$colSearch1 = ""; //검색필드명1
//$colSearch2 = ""; //검색필드명2
//$colSearch3 = ""; //검색필드명3
//$colSearch4 = ""; //검색필드명4
//$colSearch5 = ""; //검색필드명5
$cptitle = "나의 콘텐츠";
//$cpCopyright = "copyright all rights reserved"; //카피라잇

If ($pagesize == ""){
        $pagesize = 10;
}
If ($page == ""){
        $page = 1;
}

$strSearch = "";
If ($colSearch1 != "" and $stext1 != ""){
    $strSearch = $strSearch." and ".$colSearch1." like '%".$stext1."%'";
}
If ($colSearch2 != "" and $stext2 != ""){
    $strSearch = $strSearch." and ".$colSearch1." like '%".$stext2."%'";
}
If ($colSearch3 != "" and $stext3 != ""){
    $strSearch = $strSearch." and ".$colSearch1." like '%".$stext3."%'";
}
If ($colSearch4 != "" and $stext4 != ""){
    $strSearch = $strSearch." and ".$colSearch1." like '%".$stext4."%'";
}
If ($colSearch5 != "" and $stext5 != ""){
    $strSearch = $strSearch." and ".$colSearch1." like '%".$stext5."%'";
}

    If ($strSearch != ""){
        If (strpos($sqlRS, "order by") > 0) {
            If (strpos($sqlRS, "where") > 0) {
                $sqlRS = str_replace("order by", " ".$strSearch." order by", $sqlRS);
            } else {
                $sqlRS = str_replace("order by", "where 1=1 ".$strSearch." order by", $sqlRS);
            }
        } else {
            If (strpos($sqlRS, "where") > 0) {
                $sqlRS = $sqlRS." ".$strSearch;
            } else {
                $sqlRS = $sqlRS." where 1=1 ".$strSearch;
            }
        }
    }


    If ($id != "") {

        If (strpos($sqlRS, "order by") > 0) {
            If (strpos($sqlRS, "where") > 0) {
                $sqlRS = str_replace("order by", "and ".$colID." = '".$id."'"." order by", $sqlRS);
            } else {
                $sqlRS = str_replace("order by", "where ".$colID." = '".$id."'"." order by", $sqlRS);
             }
        } else {
            If (strpos($sqlRS, "where") > 0) {
                $sqlRS = $sqlRS." and ".$colID." = '".$id."'";
            } else {
                $sqlRS = $sqlRS." where ".$colID." = '".$id."'";
            }
        
        }
    }

//갯수구하기 시작
        $connect = mysql_connect($host,$user,$passwd) or die("데이터베이스에 연결할 수 없습니다.");
        mysql_select_db($user_db,$connect);
        $result = mysql_query($sqlRS,$connect);
        $totalcount = mysql_affected_rows();
        

//갯수구하기 끝
        If ($id == "") {
        $sqlRS = $sqlRS." limit ".($page * $pagesize - $pagesize). ", ".$pagesize;
    }
        $strItem = "";
        $strItem = "    <itemcount>".$totalcount."</itemcount>";


        $result = mysql_query($sqlRS,$connect);
        $itemcount = mysql_affected_rows();
        
        $start = 0;


        for($i=$start ; $i<$start+$pagesize ; $i++)
        {
                if ($i < $itemcount){
                        mysql_data_seek($result,$i);
                        $row = mysql_fetch_array($result);
        $strItem = $strItem.$newLine."    <item>";
        $strItem = $strItem.$newLine."     <id>".$row[$colID]."</id>";
        $strItem = $strItem.$newLine."     <number>".($totalcount - $pagesize * ($page - 1) - $i)."</number>";
        $strItem = $strItem.$newLine."     <title><![CDATA[".$row[$colTitle]."]]></title>";
        If ($colDate != "") {
            $strItem = $strItem.$newLine."     <date>".$row[$colDate]."</date>";
        }
        If ($colWriter != "") {
            $strItem = $strItem.$newLine."     <writer>".$row[$colWriter]."</writer>";
        }
        If ($colEmail != "") {
            $strItem = $strItem.$newLine."     <email>".$row[$colEmail]."</email>";
        }
        If ($colHomepage != "") {
            $strItem = $strItem.$newLine."     <homepage>".$row[$colHomepage]."</homepage>";
        }
        If ($colDescription != "") {

            $strItem = $strItem.$newLine."     <description><![CDATA[";
            If ($colDescriptionType == "") {
                $strItem = $strItem.$newLine.str_replace($newLine, "<br>", $row[$colDescription]);
            } elseif($row[$colDescriptionType] == $valDescriptionText)
                        {
                $strItem = $strItem.$newLine.str_replace($newLine, "<br>", $row[$colDescription]);
            } else {
                $strItem = $strItem.$newLine.$row[$colDescription];
            }
            $strItem = $strItem.$newLine."     ]]></description>";


        }
        If ($colImage1 != "") {
            $strItem = $strItem.$newLine."     <image1>".$row[$colImage1]."</image1>";
        }
        If ($colImage2 != "") {
            $strItem = $strItem.$newLine."     <image2>".$row[$colImage2]."</image2>";
        }
        If ($colOption1 != "") {
            $strItem = $strItem.$newLine."     <option1><![CDATA[".$row[$colOption1]."]]></option1>";
        }
        If ($colOption2 != "") {
            $strItem = $strItem.$newLine."     <option2><![CDATA[".$row[$colOption2]."]]></option2>";
        }
        If ($colOption3 != "") {
            $strItem = $strItem.$newLine."     <option3><![CDATA[".$row[$colOption3]."]]></option3>";
        }
        If ($colOption4 != "") {
            $strItem = $strItem.$newLine."     <option4><![CDATA[".$row[$colOption4]."]]></option4>";
        }
        If ($colOption5 != "") {
            $strItem = $strItem.$newLine."     <option5><![CDATA[".$row[$colOption5]."]]></option5>";
        }
        
        $strItem = $strItem.$newLine."    </item>";
        
                }
        }

        $newLine = "\r\n";
        $strXML = "<?xml version=\"1.0\" encoding=\"euc-kr\" ?>";
        $strXML = $strXML.$newLine."<ksncps version=\"1.0\">";
        $strXML = $strXML.$newLine." <channel>";
        $strXML = $strXML.$newLine."    <title>".$cpTitle."</title>";
        $strXML = $strXML.$newLine."    <link>".$cpLink."</link>";
        $strXML = $strXML.$newLine."    <description>".$cpDescription."</description>";
        $strXML = $strXML.$newLine."    <copyright>".$cpCopyright."</copyright>";
        $strXML = $strXML.$newLine.$strItem;
        $strXML = $strXML.$newLine." </channel>";
        $strXML = $strXML.$newLine."</ksncps>";


        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type: text/xml; charset=euc-kr");
        echo $strXML;
?>