0

配列を取り、配列内の各要素に対して csv ファイルを生成するこの PHP スクリプトを作成しました。残念ながら、何かが間違っています。指定されたディレクトリにファイルは保存されません。ただし、エラーも返されません。多分誰かが問題を見ることができますか?

$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 に答える