1. 2007.08.19 XML 생성 Class
  2. 2007.08.18 Class를 이용한 DB Connection

XML 생성 Class

<?php
class Xml_Writer {
    var $xml;
    var $indent;
    var $stack = array();
    function XmlWriter($indent = '  ') {
        $this->indent = $indent;
        $this->xml = ''."\n";
    }
    function _indent() {
        for ($i = 0, $j = count($this->stack); $i < $j; $i++) {
            $this->xml .= $this->indent;
        }
    }
    function push($element, $attributes = array()) {
        $this->_indent();
        $this->xml .= '<'.$element;
        foreach ($attributes as $key => $value) {
            $this->xml .= ' '.$key.'="'.htmlentities($value).'"';
        }
        $this->xml .= ">\n";
        $this->stack[] = $element;
    }
    function element($element, $content, $attributes = array()) {
        $this->_indent();
        $this->xml .= '<'.$element;
        foreach ($attributes as $key => $value) {
            $this->xml .= ' '.$key.'="'.htmlentities($value).'"';
        }
        $this->xml .= '>'.htmlentities($content).''."\n";
    }
    function emptyelement($element, $attributes = array()) {
        $this->_indent();
        $this->xml .= '<'.$element;
        foreach ($attributes as $key => $value) {
            $this->xml .= ' '.$key.'="'.htmlentities($value).'"';
        }
        $this->xml .= " />\n";
    }
    function pop() {
        $element = array_pop($this->stack);
        $this->_indent();
        $this->xml .= "\n";
    }
    function getXml() {
        return $this->xml;
    }
}

$xml = new Xml_Writer();
$array = array(
    array('monkey', 'banana', 'Jim'),
    array('hamster', 'apples', 'Kola'),
    array('turtle', 'beans', 'Berty'),
);

$xml->push('zoo');
foreach ($array as $animal) {
    $xml->push('animal', array('species' => $animal[0]));
    $xml->element('name', $animal[2]);
    $xml->element('food', $animal[1]);
    $xml->pop();
}
$xml->pop();

print $xml->getXml();
?>

'WebDevelop > PHP' 카테고리의 다른 글

PHP란 무엇인가...  (0) 2007.09.17
간단한 페이징 처리  (0) 2007.08.31
history.back()해도 폼값 남아 있기  (0) 2007.08.18

Class를 이용한 DB Connection

PHP에서 Class 를 써보자!
이 Class는 DB 연동부분을 클래스로만든것입니다.
다들아시겠지만 Mysql_fetch_array, mysql_fetch_Object 등등 DB 연동 함수들을 이용한것들이지요

CLASS dbconn {
    var $conn;
    function dbconn($host, $user, $pass, $dbname) {
        $this->conn = mysql_connect($host, $user, $pass) or die('connect Error');
        $status = mysql_select_db($dbname, $this->conn);
        if (!$status) {
            alert_msg('connect Error');
        }
    }

    function query($qry) {
        $result = mysql_query($qry, $this->conn);
        if (!$result) {
            alert_msg($qry);
        }
        return $result;
    }

    function num_rows($qry) {
        $result = $this->query($qry);
        $num = mysql_num_rows($result);
        return $num;
    }

    function fetch_array($qry) {
        $num_row = $this->num_rows($qry);
        if ($num_row == 0) {
            return 0;
        } else {
            $result = $this->query($qry);
            $i = 0;
            while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $arydata[$i] = $rows;
                $i++;
            }
            return $arydata;
        }
    }

    function fetch_rows($qry) {
        $num_row = $this->num_rows($qry);
        if ($num_row == 0) {
            return 0;
        } else {
            $result = $this->query($qry);
            $i = 0;
            while ($rows = mysql_fetch_row($result)) {
                $rowdata[$i] = $rows;
                $i++;
            }
            return $rowdata;
        }
    }

    function sql_result($qry,$int) {
        $result = $this->query($qry);
        $value = @mysql_result($result, 0, $int);
        return $value;
    }

    function fetch_row($qry) {
        $num_row = $this->num_rows($qry);
        if ($num_row == 0) {
            return 0;
        } else {
            $result = $this->query($qry);
            $rows = mysql_fetch_row($result);
            return $rows;
        }
    }

    function fetch_1array($qry) {
        $num_row = $this->num_rows($qry);
        if ($num_row == 0) {
            return 0;
        } else {
            $result = $this->query($qry);
            $rows = mysql_fetch_array($result);
            return $rows;
        }
    }
}

맨위에 dbconn ---> 클래스 이름입니다.
일단 클래스 안의 내용 보다도 클래스를 어떻게 사용하는지 보면 클래스를 사용하기위해서는 적당한 변수를 먼저 정해서...
$conn 이렇게 정했습니다 .

그럼 클래스를 사용해보지요.

$conn = New dbconn(DB호스트, DB사용자 계정, DB패스워드, 사용 DB명);

이렇게 필요한 인자들을 넣은후 호출합니다.
이때 $conn 변수는 클래스의 함수에 접근할 수 있는 객체 변수로 정의됩니다.

$conn 이라는 클래스 객체 변수를 이용.. 함수를 호출해봅니다.
클래스 내에 맨위의 첫번째 함수 이름을 보면 클래스명과 같은 것을 볼 수 있습니다.
이 함수는 생성자 함수이며... 클래스의 객체가 생성될때마다 자동으로 호출되어 집니다.

$conn = New dbconn(DB호스트, DB사용자 계정, DB패스워드, 사용 DB명);

이렇게 객체를 생성할때 이미 저 생성자 함수는 호출된것이지요...
그러므로 생성자 함수에 DB Connect 부분을 코딩하였습니다... 클래스를 호출했다면 이미 디비 연결된것이지요

그럼 생성된 객체로 클래스를 이용해봅시다.
클래스 내의 함수를 보면 fetch_array 함수가 있을것입니다
일단 클래스 내의 함수를 호출해 사용할려면...

$data = $conn->fetch_array($sql);

형식은 이렇게 됩니다.
$conn 객체변수로 클래스의 fetch_array 함수를 호출하여 $data 라는 변수에 넣어 줍니다
그럼 함수 fetch_array 를 보면 결과를 배열 반환합니다

$data 라는변수에는 쿼리를 날려 결과로 얻은 레코드들이 들어있지요..
그럼 만약 게시판 코딩 상에서 이 레코드들을 뿌린다면

for ($i = 0; $i < count($data); $i++) {
    echo $data[$i]['컬럼명1'];
    echo $data[$i]['컬럼명2'];
    echo $data[$i]['컬럼명3'];
                    .
                    .
                    .
                    .
}

이렇게 하면 ~~~ 쭉~~

'WebDevelop > JavaScript' 카테고리의 다른 글

inputbox 동적생성  (0) 2007.09.11
JavaScript 기본  (0) 2007.09.11
크로스 브라우징때 편할것 같은 DOM  (0) 2007.08.23
Return top