Merhaba, Ziyaretçi. Lütfen giriş yapın veya üye olun.
Aktivasyon mailiniz gelmediyse buraya tıklayın.
09, 2008, 02:33:51 am
42744 Mesaj 8090 Konu Gönderen: 17931 Üye
Son üye: medist
Turk-Php.Com Forum  |  Yardım Forumları  |  PHP Yardım Forumu  |  PHP Class Deposu  |  Mysql den CVS oluşturan Class « önceki sonraki »
Sayfa: [1] Yazdır
Yazan Konu: Mysql den CVS oluşturan Class  (Okunma Sayısı 620 defa)
phparmy
phparmy
PHP Stajyeri
**
Offline Offline

Mesaj Sayısı: 371


Elektronik imzam.


Üyelik Bilgileri
Mysql den CVS oluşturan Class
« : 06, 2006, 04:04:46 am »

Class dosyası
Kod:
<?php
###########################################################
# CLASS:    rs2csv
# Author:   Sean S. Natoewal
# Contact:  sean@natoewal.nl
# Date:     June 22th 2003
# Version:  1.0
#
# This class can be used to convert a resultset which has 
# been generated by PHP's built-in MySQL functions to a 
# comma separated values (CSV) file.
#
# If you find this script useful or are using it for one of 
# your projects please send me an email telling me a little 
# about the project and how useful the script was to you.

# If you got any questions or comments feel free to mail me.
#
############################################################
class rs2csv {
    var 
$_str$_fname$_sep$_rs$_link$_ctype$_cdisp$_con false;
    
// constructor
    
function rs2csv() {
        
$this->_str "";
        
$this->set_fname("file.csv");
        
$this->set_ctype("text/tab-separated-values"); 
        
$this->set_cdisp("attachment");
        
$this->set_sep(",");  
    }
    
// function to set the filename
    
function set_fname($fname) {
        
$this->_fname $fname;   
    }
    
// function to set the content-type
    
function set_ctype($ctype) {
        
$this->_ctype $ctype;
    }
    
// function to set the content-disposition
    
function set_cdisp($cdisp) {
            
$this->_cdisp $cdisp;
        }
    
// function to set the separator
    
function set_sep($sep) {
        
$this->_sep $sep;
    }
    
// function to open a connection to the MySQL server
    
function make_con($server$username$password$database_name) {
        
$this->_link mysql_connect($server$username$password) or $this->display_err("Could not connect : " mysql_error());
        
mysql_select_db($database_name$this->_link) or $this->display_err("Could not select database");    
        
$this->_con true;
    }
    
// function to close the MySQL connection
    
function close_con() {
        
// free resultset 
        
mysql_free_result($this->_rs);
        
// closing connection
        
if ($this->_con
            
mysql_close($this->_link);
    }
    
// function to execute the SQL-query
    
function exec_sql($sql$link "") {
        if (!
$this->_con
            
$this->_link $link;
        if (
$this->_link == "" || !$this->_con
            
$this->_rs mysql_query($sql) or $this->display_err("Query failed : " mysql_error());
        else 
            
$this->_rs mysql_query($sql$this->_link) or $this->display_err("Query failed : " mysql_error());
        
$this->process_rs($this->_rs);
        
$this->close_con();
    }
    
// function to process the resultset ($rs)
    
function process_rs($rs) {
        
$this->_rs $rs;
        if (
mysql_num_fields($this->_rs) == 0
            
$this->display_err("Invalid resultset. Number of fields is 0.");
        if (
mysql_num_rows($this->_rs) == 0
            
$this->display_err("Invalid resultset. Number of records is 0.");
        for (
$i=0;$i<mysql_num_fields($this->_rs);$i++) { 
            
$this->_str .= mysql_field_name($this->_rs$i).$this->_sep;
        }
        
$this->_str .= "\n";
        while (
$line mysql_fetch_array($this->_rsMYSQL_ASSOC)) {
            foreach (
$line as $col_value) {
                
$this->_str .= $col_value.$this->_sep;
            }
            
$this->_str .= "\n";
        }
    }
    
// function to display an error message
    
function display_err($err_msg) {
        echo 
"Error: $err_msg";
        exit;
    }
    
// function to output the CSV file
    
function output_csv() {
        if (
strlen($this->_str) == 0)
            
$this->display_err("Empty file");
        
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
        
header("Cache-Control: no-store, no-cache, must-revalidate");
        
header("Cache-Control: post-check=0, pre-check=0"false);
        
header("Pragma: no-cache");
        
header("Content-Length: ".strlen($this->_str));
        
header("Content-type: ".$this->_ctype);
        
header("Content-Disposition: ".$this->_cdisp."; filename=".$this->_fname);
        echo 
$this->_str;
        exit;
    }
}
?>

Kullanım şekli
Kod:
<?php
/*
Example 1
*/
require("rs2csv.class.php");
$csv = new rs2csv// create a new instance of the rs2csv class.
$csv->set_fname("example1.csv"); // Set the filename for download. Default is 'filename.csv'.
$csv->set_ctype("application/octet-stream"); // Set the content-type for download. Default is 'text/tab-separated-values'.
$csv->set_cdisp("attachment"); // Set the content-disposition for download. Default is 'attachment'.
$csv->set_sep(";"); // Set the seperator sign. Default is ','.
$csv->make_con("localhost""root""""mysql"); // Open a database connection to the MySQL server.
$csv->exec_sql("SELECT * FROM user"); // Execute your SQL-query.
$csv->output_csv(); // Output the CSV file.
?>

Logged
Sayfa: [1] Yazdır 
« önceki sonraki »
Gitmek istediğiniz yer:  


Turk-Php.Com Forum | SMF Forum Software © 2005, Simple Machines LLC. All Rights Reserved.