0

我制作了这个 PHP 脚本,它应该采用一个数组,并为数组中的每个元素生成一个 csv 文件。不幸的是,出了点问题。它不存储指定目录中的任何文件。但它也不会返回任何错误。也许有人可以看到问题?

$ids = json_decode($_POST['jsonarray']); // array sent with ajax
$start = $_POST['start']; // date sent with ajax
$end = $_POST['end']; // date sent with ajax

$start_date = date('yyyy-mm-dd', strtotime($start)); // format dates to sql firendly
$end_date = date('yyyy-mm-dd', strtotime($end));

$toZip = array(); // Prepare array to files for zip

if(is_array($ids)) {
    foreach ($ids as $key => $qr)
    {
        // Get labels first
        // Here we prepare the first line in the .CSV file
        $tb = $qr . '_labels';
        $sql = $user_pdo->query("SELECT * FROM $tb");
        $head_array = array('Log ID', 'Timestamp');
        while ($row = $sql->fetch(PDO::FETCH_ASSOC))
        {
        // This array is the first line in the .CSV file
            $head_array[] = $row['label'];
        }

        // Get ready for looping through the database
        $table = $qr . '_data';
        $results = $user_pdo->prepare("SELECT * FROM $table WHERE timestamp BETWEEN :start_date AND :end_date;");
        $results->bindParam(':start_date', $start_date, PDO::PARAM_STR);
        $results->bindParam(':end_date', $$end_date, PDO::PARAM_STR);
        $results->execute();

        // Pick a filename and destination directory for the file
        $filename = "temp/db_user_export_".time().".csv";

        // Actually create the file
        // The w+ parameter will wipe out and overwrite any existing file with the same name
        $handle = fopen($filename, 'w+');

        // Write the spreadsheet column titles / labels
        fputcsv($handle, $head_array);


        // Write all the user records to the spreadsheet
        foreach($results as $row)
        {
                // amount of rows is unknown
            $rows = $row->rowCount();
            $insert_array = array();
            for ($i=0; $i<=$rows; $i++)
            {
                // function goes here
                $insert_array[] = $row[$i];
            }

            fputcsv($handle, $insert_array);
        }

        // Finish writing the file
        fclose($handle);

        $toZip[] = $filename;
    }
}

示例var_dump($ids);

array(4) {
  [0]=>
  string(5) "t23ry"
  [1]=>
  string(5) "6us32"
  [2]=>
  string(5) "se43z"
  [3]=>
  string(5) "o00gq"
}
4

2 回答 2

1

我找到了答案。找了半天,才看到这个功能

    foreach($results as $row)
    {
            // amount of rows is unknown
        $rows = $row->rowCount();
        $insert_array = array();
        for ($i=0; $i<=$rows; $i++)
        {
            // function goes here
            $insert_array[] = $row[$i];
        }

        fputcsv($handle, $insert_array);
    }

由于以下原因没有工作:

  1. $rows = $row->rowCount();必须$rows = count($row);
  2. 返回数组中的字符串数$row高于预期,因此我需要将 select 语句更改为$results = $user_pdo->query("SELECT * FROM $table WHERE timestamp >= '$start' AND timestamp <= '$end'";, PDO::FETCH_NUM);. 这只会给我按数字顺序排列的行,这将使$row[$i] -> array工作正常进行。
  3. 同样如您所见,我将准备好的语句改为查询,并将开始日期和结束日期变量更改为未格式化。

这确实花了一些时间,但它终于奏效了。非常感谢所有支持的家伙。

于 2013-10-17T17:59:36.973 回答
0

fputcsv 一次只输出一行。改变这个:

        for ($i=0; $i<=$rows; $i++)
        {
            // function goes here
            $insert_array[] = $row[$i];
        }

        fputcsv($handle, $insert_array);

对此:

        for ($i=0; $i<=$rows; $i++)
        {
            // function goes here
            fputcsv($handle, $row[$i]);
        }
于 2013-10-17T16:39:25.460 回答