使用PHP[通过cron作业运行]从mysql保存excel/cvs文件
我想导出mysql表的一些结果,我目前可以单击下载链接并下载xls,但我希望能够通过cron作业运行该表,并将每周结果通过电子邮件发送给我 我已经看过从Mysql执行此操作,并将其直接保存为csv 然而,我正在努力使用SQL,表格式如下使用PHP[通过cron作业运行]从mysql保存excel/cvs文件,mysql,csv,export-to-excel,fwrite,concrete5,Mysql,Csv,Export To Excel,Fwrite,Concrete5,我想导出mysql表的一些结果,我目前可以单击下载链接并下载xls,但我希望能够通过cron作业运行该表,并将每周结果通过电子邮件发送给我 我已经看过从Mysql执行此操作,并将其直接保存为csv 然而,我正在努力使用SQL,表格式如下 btFormQuestions (some columns ommitted) +-------+---------------+----------+-----------+ | msqID | questionSetId | Question | Input
btFormQuestions (some columns ommitted)
+-------+---------------+----------+-----------+
| msqID | questionSetId | Question | InputType |
|-------+---------------+----------+-----------+
| 1 | 123456 | Name | field |
| 2 | 123456 | Telephone| field |
| 3 | 123456 | Email | email |
| 4 | 123456 | Enquiry | test |
btFormAnswers
+-----+------+-------+-----------------+
| aID | asID | msqID | answer |
+-----+------+-------+-----------------+
| 1 | 1 | 1 | Sean |
| 2 | 1 | 2 | 0800 0 |
| 3 | 1 | 3 | se@te.com |
| 4 | 1 | 4 | Asking Question |
btFormAnswersSet
+------+---------------+---------------------+
| asID | questionSetId | created |
+------+---------------+---------------------+
| 1 | 123456 | 2013-04-30 11:07:55 |
我目前用于将信息输入PHP和数组的sql查询如下所示:
//get answers sets
$sql='SELECT * FROM btFormAnswerSet AS aSet '.
'WHERE aSet.questionSetId='.$questionSet.' ORDER BY created DESC LIMIT 0, 100;
$answerSetsRS=$db->query($sql);
//load answers into a nicer multi-dimensional array
$answerSets=array();
$answerSetIds=array(0);
while( $answer = $answerSetsRS->fetchRow() ){
//answer set id - question id
$answerSets[$answer['asID']]=$answer;
$answerSetIds[]=$answer['asID'];
}
//get answers
$sql='SELECT * FROM btFormAnswers AS a WHERE a.asID IN ('.join(',',$answerSetIds).')';
$answersRS=$db->query($sql);
//load answers into a nicer multi-dimensional array
while( $answer = $answersRS->fetchRow() ){
//answer set id - question id
$answerSets[$answer['asID']]['answers'][$answer['msqID']]=$answer;
}
return $answerSets;
我希望能够执行以下操作之一
A.)将所有这些内容移动到一个查询中,以获得以下结果
+---------------+------+-----------+-----------+-----------------+
| QuestionSetID | Name | Telephone | Email | Enquiry |
+---------------+------+-----------+-----------+-----------------+
| 123456 | Sean | 0800 0 | se@te.com | Asking Question |
(我确实尝试了各种连接,但没有完全正确)
如果我可以让这个工作,我不会介意保存为CSV
B.)将返回的数组作为excel文件输出,该文件可以保存到服务器上的某个位置。
当前代码从数组创建一个html表
代码有点长,所以我只在这里粘贴顶部和底部
//fwrite($handle, $excelHead);
//fwrite($handle, $row);
//fflush($handle);
ob_start();
header("Content-Type: application/vnd.ms-excel");
echo "<table>\r\n";
//Question headers go here
foreach($answerSets as $answerSetId=>$answerSet){
$questionNumber=0;
$numQuestionsToShow=2;
echo "\t<tr>\r\n";
echo "\t\t<td>". $dateHelper->getSystemDateTime($answerSet['created'])."</td>\r\n";
foreach($questions as $questionId=>$question){
$questionNumber++;
if ($question['inputType'] == 'checkboxlist'){
$options = explode('%%', $question['options']);
$subanswers = explode(',', $answerSet['answers'][$questionId]['answer']);
for ($i = 1; $i <= count($options); $i++)
{
echo "\t\t<td align='center'>\r\n";
if (in_array(trim($options[$i-1]), $subanswers)) {
// echo "\t\t\t".$options[$i-1]."\r\n";
echo "x";
} else {
echo "\t\t\t \r\n";
}
echo "\t\t</td>\r\n";
//fwrite($handle, $node);
//fflush($handle);
}
}elseif($question['inputType']=='fileupload'){
echo "\t\t<td>\r\n";
$fID=intval($answerSet['answers'][$questionId]['answer']);
$file=File::getByID($fID);
if($fID && $file){
$fileVersion=$file->getApprovedVersion();
echo "\t\t\t".'<a href="'. $fileVersion->getDownloadURL() .'">'.$fileVersion->getFileName().'</a>'."\r\n";
}else{
echo "\t\t\t".t('File not found')."\r\n";
}
echo "\t\t</td>\r\n";
}else{
echo "\t\t<td>\r\n";
echo "\t\t\t".$answerSet['answers'][$questionId]['answer'].$answerSet['answers'][$questionId]['answerLong']."\r\n";
echo "\t\t</td>\r\n";
}
//fwrite($handle, $node);
//fflush($handle);
}
echo "\t</tr>\r\n";
//fwrite($handle, $row);
//fflush($handle);
}
echo "</table>\r\n";
//fwrite($handle, $excelFoot);
//fflush($handle);
//fclose($handle);
file_put_contents($filePath, ob_get_clean());
//fwrite($handle,$excelHead);
//fwrite($handle,$row);
//fflush($handle);
ob_start();
标题(“内容类型:application/vnd.ms excel”);
回显“\r\n”;
//问题标题转到这里
foreach($answerSet作为$answerSetId=>$answerSet){
$questionNumber=0;
$numQuestionsToShow=2;
回显“\t\r\n”;
echo“\t\t”。$dateHelper->getSystemDateTime($answerSet['created'])。“\r\n”;
foreach($questionId=>$question的问题){
$questionNumber++;
如果($question['inputType']=='checkboxlist'){
$options=explode('%%',$question['options']);
$subanswers=explode(“,”,$answerSet['answers'][$questionId]['answer']);
对于($i=1;$i getApprovedVersion();
回显“\t\t”.“\r\n”;
}否则{
回显“\t\t\t”.t('未找到文件')。”\r\n;
}
回显“\t\t\r\n”;
}否则{
回显“\t\t\r\n”;
echo“\t\t\t”。$answerSet['answers'][$questionId]['answer'].$answerSet['answers'][$questionId]['answerLong'.]”\r\n;
回显“\t\t\r\n”;
}
//fwrite($handle,$node);
//fflush($handle);
}
回显“\t\r\n”;
//fwrite($handle,$row);
//fflush($handle);
}
回显“\r\n”;
//fwrite($handle,$excelFoot);
//fflush($handle);
//fclose($handle);
文件内容($filePath,ob_get_clean());
我可以将文件保存到目录中,但我在将其设置为Excel文件时遇到问题,我也尝试过,使用Fwrite(而不是缓冲区)得到类似的结果
有人能帮忙吗,或者给我指一下正确的位置
谢谢,,
肖恩(Sean)我会在Concrete 5中这样做。这样你就可以得到所有与表单结果相关的模型,以及各种帮助工具(如电子邮件) 有关作业的更多信息,请参阅。要从cron作业运行,请参阅 看起来您已经有了生成答案的代码,并将其放入数组中,但您可能希望看到类似的内容。我不确定这正是您所需要的,但我知道仪表板页面为您构建了答案表,因此代码显然存在于某个地方 最后,要创建excel文件,请使用“将其放入表中并调用它.xls”方法,该方法适用于excel和open office。我不确定“将其设置为excel有问题”的确切含义,但听起来这似乎是您目前的问题。如果某个内容被保存到文件中,则您应该发布文件内容,您/我们可以反向查找导致该问题的原因。可能只是格式错误的HTML或其他内容
最后,要发送电子邮件,您可以使用Mail Helper,但目前不允许使用附件(github中有一个pull请求,可以使用它来覆盖Mail Helper)。通常,“最佳做法”将其作为链接发送。我也计划使用mail helper将该文件作为附件发送,一旦我能够正确保存该文件。谢谢James,(编辑之前的评论时已没有时间了)我目前将此设置为仪表板中的一项作业,但是在服务器上将文件保存为xls文件时会出现一个问题,不是通过浏览器而是使用输出缓冲区将内容类型设置为ms excel似乎不起作用。创建的表是良好的html。该文件将保存为xls扩展名,但尝试打开它从Excel中发出一条文件损坏消息。该文件作为一个表放置并称为xls,但在使用输出缓冲区并将该缓冲区的内容保存到文件时不会出现。我实际上使用了BuildAnswerSetArray()中的许多代码函数当前构建答案。如果有人指出可以执行此内容类型的SQL查询纯粹是HTTP协议头,并且对输出缓冲(本身)没有影响,则返回我刚才漫游的数组或者文件如何写入磁盘。事实上,在缓冲区中使用
echo
,然后将其写入磁盘是非常奇怪的。您应该回到fwrite()
s(我只需要创建一个$file
变量并附加到该变量)。我向您保证,如果该文件以文本形式写入磁盘,并且具有相应的表代码,那么Excel将可以很好地读取该文件。请在文本编辑器中打开该文件,并将文本发布到pastie.org或其他网站。