Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想将记录从SQL导入Excel_Php_Mysql_Excel - Fatal编程技术网

Php 我想将记录从SQL导入Excel

Php 我想将记录从SQL导入Excel,php,mysql,excel,Php,Mysql,Excel,是的,有很多标题,但我选择在这个标题下解释问题,因为我在其他标题中找不到解决方案:) 数据库包含调查问题和本次调查答案的记录。我想在第一列中打印这些记录的问题标题,并在各列下打印答案 比如说, 问题表id问题\u id问题 答案表id问题\u id答案 我尝试了许多方法,但没有在问题标题下的每一行显示答案 Excel, A - B - C .. etc. 1 - question_title question_title q

是的,有很多标题,但我选择在这个标题下解释问题,因为我在其他标题中找不到解决方案:)

数据库包含调查问题和本次调查答案的记录。我想在第一列中打印这些记录的问题标题,并在各列下打印答案

比如说,

问题表id问题\u id问题

答案表id问题\u id答案

我尝试了许多方法,但没有在问题标题下的每一行显示答案

Excel,
    A       -        B       -         C .. etc.
1 - question_title   question_title    question_title .. etc.

2 - answer_title     answer_title      answer_title   .. etc.
3 - answer_title     answer_title      answer_title   .. etc.
4 - answer_title     answer_title      answer_title   .. etc.
我做了什么:)

我需要在问题下面列出答案 结果:

从这里开始

我个人使用此函数从任何数组创建CSV内容

function array2csv(array &$array)
{
   if (count($array) == 0) {
     return null;
   }
   ob_start();
   $df = fopen("php://output", 'w');
   fputcsv($df, array_keys(reset($array)));
   foreach ($array as $row) {
      fputcsv($df, $row);
   }
   fclose($df);
   return ob_get_clean();
}
然后,您可以使用以下方法让用户下载该文件:

function download_send_headers($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");
}
Usa
通用电气示例:

download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($array);
die();

您可以为当前的表结果视图做一个示例屏幕截图吗。结果:(我仍然不清楚您的要求是什么。您可以给出一个固定查询的示例(
,其中question_id=1
),但您谈到打印多个问题。能否提供
$quest
$answer
变量(一旦获取)的数据示例?也许可以看看前面的问题。。。。
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($array);
die();