コード例 #1
0
/**
 * Writes a single row to a CSV file, merging headers before writing, if needed.
 * @param array $array The row to write to the file.
 * @param string $filename The path to the file to write to.
 * @param string $delim A single character noting the delimiter in the file.
 * @see readCsv()
 * @see writeCsv()
 */
function writeLineToFile(array $array, $filename, $delim = ',')
{
    if (!FileExists($filename)) {
        // file doesn't exist, write away
        $file = fForceOpen($filename, "wb");
        fputcsv($file, array_keys($array), $delim);
        fputcsv($file, $array, $delim);
    } else {
        // file already exists, need to merge headers before writing
        $data = readCsv($filename, $delim);
        $headers = array_flip($data[0]);
        $newHeaders = array_diff_key($array, $headers);
        if (count($newHeaders) > 0) {
            $headers = $headers + $newHeaders;
            $data[0] = array_keys($headers);
            $data[] = SortArrayLikeArray($array, $headers);
            writeCsv($filename, $data, $delim);
        } else {
            writeCsv($filename, array(SortArrayLikeArray($array, $headers)), $delim, true);
        }
    }
    return $array;
}
コード例 #2
0
ファイル: dbWork.php プロジェクト: agolomazov/LoftSchool-1
<?php

require "dbConnect.php";
require "personalFunctions.php";
if (isset($_POST['table']) && isset($_POST['format'])) {
    $sql = 'SELECT * FROM ' . $_POST['table'];
    $result = $dbo->query($sql);
    $rows = $result->fetchAll(PDO::FETCH_ASSOC);
    unset($dbo);
    switch ($_POST['format']) {
        case 'csv':
            writeCsv($rows);
            break;
        case 'json':
            writeJson($rows);
            break;
        case 'xml':
            writeXml($rows);
            break;
    }
} else {
    $result = $dbo->query('SHOW TABLES');
    unset($dbo);
}
コード例 #3
0
ファイル: ExportCsv.php プロジェクト: annProg/yourCMDB
if (isset($exportOptions['delimiter'])) {
    $delimiter = $exportOptions['delimiter'];
}
if (isset($exportOptions['enclosure'])) {
    $enclosure = $exportOptions['enclosure'];
}
//send header
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=output.csv");
//open filehandler
$outstream = fopen("php://output", 'w');
//generate CSV header
$csvHeader = array();
$csvHeader[] = gettext("AssetID");
$csvHeader[] = gettext("Active");
foreach ($fieldnames as $fieldname) {
    $csvHeader[] = $fieldname;
}
writeCsv($outstream, $csvHeader, $delimiter, $enclosure);
//generate CSV output data
foreach ($objects as $object) {
    $dataset = array();
    $dataset[] = $object->getId();
    $dataset[] = $object->getStatus();
    foreach ($fieldnames as $fieldname) {
        $dataset[] = $object->getFieldValue($fieldname);
    }
    writeCsv($outstream, $dataset, $delimiter, $enclosure);
}
//close filehandler
fclose($outstream);