2017-03-03 69 views
-1

我有工作代码将记录导出到JSON(请参见下文),现在我需要将记录导出为CSV。 下面的代码有“根节点”,这意味着它将导出PARENTID = 2的成员及其所有子节点(递归地)。 我需要的是导出的是给定PARENTID的记录,但使用CSV代替JSON。PHP导出为CSV而不是JSON

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "mydataabse"; 
    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $index = array(); 
    $sql = "SELECT NAME, ID, PARENTID FROM mytable"; 
    $result = $conn->query($sql); 

    while($row = $result->fetch_array(MYSQLI_ASSOC)){ 
     $rows[] = $row; 
     $index[$row['ID']] = $row; 
    } 

    // build the tree 
    foreach($index as $id => &$row){ 
     if ($id === 0) continue; 
     $parent = $row['PARENTID']; 
     $index[$parent]['children'][] = &$row; 
    } 
    unset($row); 

    // root node - exported are members with this PARENTID and all they children's 
    $index = $index[2]['children']; 

    /* free result set */ 
    $result->close(); 
    /* close connection */ 
    $conn->close(); 

    // output json 
    header('Content-Type: application/json'); 
    echo json_encode($index, JSON_PRETTY_PRINT); 

这里是mytable的结构,如果需要的话:

ID PARENT NAME 
1  0  John Doe 
2  1  Sally Smith 
3  2  Mike Jones 
4  3  Jason Williams 
5  4  Sara Johnson 
6  1  Dave Wilson 
7  2  Amy Martin 

太谢谢你了。

回答

0

您可以使用本机的功能fputcsv这样例如

编辑:包括你的代码

<?php 

$rows = array(
array('id' => 1, 'Parent' => 0, 'name' => 'John Doe'), 
array('id' => 2, 'Parent' => 1, 'name' => 'Sally Smith'), 
array('id' => 3, 'Parent' => 2, 'name' => 'Mike Jones'), 
array('id' => 4, 'Parent' => 3, 'name' => 'Jason Williams'), 
array('id' => 5, 'Parent' => 4, 'name' => 'Sara Johnson'), 
array('id' => 6, 'Parent' => 1, 'name' => 'Dave Wilson'), 
array('id' => 7, 'Parent' => 2, 'name' => 'Amy Martin'), 
); 
// create an index on id 
$index = array(); 

foreach($rows as $row){ 
    $index[$row['id']] = $row; 
} 

// build the tree 
foreach($index as $id => &$row){ 
    if ($id === 0) continue; 
    $parent = $row['Parent']; 
    $index[$parent]['children'][] = &$row; 
} 
unset($row); 

// obtain root node 
$index = $index[2]['children']; 

// Construct the final datas 
$columns = array('Name', 'Id', 'ParentID'); 

$datas = array($columns); 
foreach($index as $data) { 
    $datas[] = array(
     $data['name'], 
     $data['id'], 
     $data['Parent'] 

    ); 
} 

// Tell the client to download file 
headerDownload('mySuperFile.csv'); 

// Export the CSV to standard output 
exportToCSV('php://output', $datas); 

function exportToCSV($file, array $values, $delimiter = ";", $enclosure = '"', $escape = "\\") 
{ 
    $handler = @fopen($file, 'w'); 

    if(! $handler) { 
     throw new Exception(sprintf("Unable to open stream '%s'", $file)); 
    } 

    foreach ($values as $value) { 
     fputcsv($handler, $value, $delimiter, $enclosure, $escape); 
    } 
    return fclose($handler); 
} 

function headerDownload($filename) 
{ 
    // disable caching 
    $now = gmdate("D, d M Y H:i:s"); 
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT"); 
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); 
    header("Last-Modified: {$now} GMT"); 

    // force download 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 

    // disposition/encoding on response body 
    header("Content-Disposition: attachment;filename={$filename}"); 
    header("Content-Transfer-Encoding: binary"); 
} 
+0

一个完整的例子非常感谢你,但我需要把这个代码?我是PHP新手,如果你可以编写包含上面代码的简单示例,我将不胜感激。 – ZMik

+0

我刚编辑我的回复。你可以检查它是否工作?它不是,尝试注释行headerDownload('mySuperFile.csv');并告诉我输出 –

+0

你好,非常感谢你,这里有错误:
警告:array_unshift()预计参数1是阵列,在C语言的零式给出:\ APPS \ CSV.php上线

捕获的致命错误:传递给exportToCSV(参数2)必须是类型的数组,空给出,称为在C:\ APPS \ CSV.php上线76和在C中定义:\ APPS \ CSV.php on line
ZMik