额外字段未打印

时间:2016-03-15 20:24:22

标签: php excel csv cakephp export-to-excel

下午好。

我目前正在使用CakePHP为评判系统添加更多功能和功能。我已经寻找可能的解决方案以及自己尝试解决问题,但我仍然无法弄清楚我的代码或数据库有什么问题。我正在尝试添加一个额外的字段,以便从我的数据库中的excel文件中输出它,但它不会被打印。 Jcategory填入数据库,但不会打印在CSV文件中。除了我的添加(Jcategory)之外,所有其他字段都填写在excel文件中。

public function data2()
{ 
    $this->autoLayout = $this->autoRender = false;
    ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
    //create a file
    $filename = "export_".date("Y.m.d").".csv";

    $csv_file = fopen('php://output', 'w');
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'"');

    //ALWAYS NEED RESULT
    $results = $this->Post->find('all');
    //$results = $this->Poster->find('all');

    // The column headings of your .csv file
    $header_row = array("id","poster_id","criteria1","criteria2","criteria3","criteria4","criteria5","criteria6","criteria7","criteria8","criteria9","criteria10","Jcategory","Test","Score","Author","category");
    fputcsv($csv_file,$header_row,',','"');
    // Each iteration of this while loop will be a row in your .csv file where each field corresponds to the heading of the column
    foreach($results as $result)
    {
        $authors = '';
        $authors = substr($authors,0,sizeof($authors)-3);
        $row = array(
            $result['Post']['id'],
            $result['Post']['poster_id'],
            $result['Post']['criteria1'],
            $result['Post']['criteria2'],
            $result['Post']['criteria3'],
            $result['Post']['criteria4'],
            $result['Post']['criteria5'],
            $result['Post']['criteria6'],
            $result['Post']['criteria7'],
            $result['Post']['criteria8'],
            $result['Post']['criteria9'],
            $result['Post']['criteria10'],
            $result['Post']['comments'],
            $result['Post']['jcategory'], //This is not being printed in the CSV File
            //Results are here at the bottom
            ($result['Post']['criteria1'])+($result['Post']['criteria2'])+($result['Post']['criteria3'])+($result['Post']['criteria4'])+($result['Post']['criteria5'])+($result['Post']['criteria6'])+($result['Post']['criteria7'])+($result['Post']['criteria8'])+($result['Post']['criteria9'])+($result['Post']['criteria10']),
            $result['poster']['author'],
            $result['poster']['category'],              
        );

        foreach($result['Author'] as $author){
            array_push($row, $author['id']);
            array_push($row, $author['middle']);
            array_push($row, $author['last']);
            array_push($row, $author['email']);
            array_push($row, $author['institution']);
            array_push($row, $author['department']);
            array_push($row, $author['type']);
        }
        fputcsv($csv_file,$row,',','"');
    }

    fclose($csv_file);
}

0 个答案:

没有答案