PHP Classes

File: csv_example.php

Recommend this page to a friend!
  Classes of Soltermann Paul   PHP CSV Writer Class   csv_example.php   Download  
File: csv_example.php
Role: Example script
Content type: text/plain
Description: Example use csvWriter
Class: PHP CSV Writer Class
Export tabular data records to files in CSV format
Author: By
Last change:
Date: 11 years ago
Size: 1,682 bytes
 

Contents

Class file image Download
<?php

   
include("csv_writer.inc.php");

   
$fileName = "redground.csv";

   
/*

    $fileName : fileName name of csv file to be created.

                    if you are using file name with directory i.e. test/redground.csv

                    then the directory must be existed on the system and have permissioned properly

                    to write the file.

    */

   
$excel = new csvWriter($fileName);

    if(!
$excel->fp)

    {

        echo
$excel->error;

        die;

    }

   

   
// connect to the Database

    
$user = "USER";

   
$pass = "PW"

   
$server = "localhost";

   
$dbase = "RedGround";

   
$conn = mysql_connect($server,$user,$pass) or die(mysql_error());

   
mysql_query('set names "utf8"');

   
mysql_select_db($dbase , $conn) or die(mysql_error());

   

   
// get field-names from your table

   
$sql = "SELECT * FROM ME";

   
$result = mysql_query($sql);

   
$i = 0;

   
$meta = array();

    while (
$i < mysql_num_fields($result))

    {

       
$meta = mysql_fetch_field($result, $i);

       
$names[] = $meta->name;

       
$i++;

    }

   
// write field-names in uppercase

   
$excel->writeupcLine($names);

   

   
// get data rows from your table

   
$sql = "SELECT * FROM ME ORDER BY LANG_CODE";

   
$result = mysql_query($sql);

    WHILE (
$row = mysql_fetch_assoc($result) )

    {

       
$excel->writeLine($row);

    }

   

   
// write a empty line

   
$sp = array("");

   
$excel->writeLine($sp);

   

   
// write a fantasy string

   
$eol = array("","end of", "mytable", "data", "output");

   
$excel->writeupcLine($eol);

   

    if (
$excel->close())

    {

        echo
"Data written to file $fileName Successfully.";

    }

    else

    {

        echo
"error writing $fileName ";

    }

?>