PHP使用正则表达式循环并查询mysql以输出Excel文件 [英] PHP using regex to loop and query mysql to output an excel file

查看:66
本文介绍了PHP使用正则表达式循环并查询mysql以输出Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了什么.我希望首先得到的数组可以转置.然后正则表达式分解列中的一列,以便我们可以查询数据库中的另一张表,以将列标题替换为正确的标题.正则表达式正确吗?转置正确吗?

What am I doing wrong here. I want the array we get at first to be transposed. Then the regex breakups one of the columns so we can query another table in the database to replace that column header with the correct header. Is the Regex correct? Is the transpose correct?

这将得到我们想要的查询

This will get the query we want

exportMysqlToCsv($tablename,$tokenmain, $id);
function exportMysqlToCsv($tablename,$tokenmain, $id, $filename = 'Results.csv'){
      $sql_query = "select * from $tablename where $tokenmain";

// Gets the data from the database

$result = mysql_query($sql_query);


$f = fopen('php://temp', 'wt');
$first = true;

$temp = mysql_fetch_array($result);

我希望这可以转置我们刚得到的数组.不确定这里

I hope this transposes the array we just got . Not sure here

array_unshift($temp, null);
$transposed_array = call_user_func_array('array_map', $temp);

然后,我们遍历转置数组,替换接收到的列标题(例如111X2222X3333),并将其拆分以查询要替换为的标题并将其输出到excel文件.这是我很困惑的地方.

Then we loop through transposed-array an replace the column header we receive (ex. 111X2222X3333) and split these up to query the headers we want to replace them with and output it to the excel file. This is where I am very confused.

for ($i = 0; $i < $count; $i++) {
if ($transposed_array[$i][0])//starts with a number
{
    $sid = regexp /^[0-9]*/

    $gid = regexp /X[0-9]*X/
    //remove first and last character from $gid
    $gid = str_replace("X", "", $gid)

    $qid = regexp /[0-9]*$/

    $question_text = mysql_query("select question from lime_questions where sid =           ".$sid." and gid = ".$gid." and qid = ".$qid." limit 1");
    $transposed_array[$i][5] = $question_test
}

这应该将数组输出到csv文件

This should output the array to the csv file

while ($row = mysql_fetch_assoc($result))
{
         if ($first)
     {
            fputcsv($f, array_keys($row));
            $first = false;
     }
fputcsv($f, $row);

} // end while

//恢复正常处理

$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");

//以适当的mime类型输出到浏览器,选择;)

// Output to browser with appropriate mime type, you choose ;)

header("Content-type: text/x-csv");
header("Content-type: text/csv");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");

fpassthru($f);

exit;

推荐答案

由于以下内容不是有效的PHP表达式,因此不确定要实现的目标

Not sure what you want to achieve because the folloing are not valid PHP expressions

  $sid = regexp /^[0-9]*/
  $gid = regexp /X[0-9]*X/
  $qid = regexp /[0-9]*$/

但是请发表您的评论

我希望此循环将我们从第一个数组中接收到的列标题拆分开.列标题的示例是111X222X3333.我想使用正则表达式将它们分开,所以每个都是自己的属性,例如sid = 111 gid = 222和qid = 3333

I want this loop to split up the column headers we recieve from the first array. An example of a column header is 111X222X3333. I want to split them up using regex so each one is its own attribute like sid =111 gid=222 and qid=3333

您需要的只是

$string = "111X222X3333" ;
list($sid,$gid,$qid) = explode("X", $string);

这篇关于PHP使用正则表达式循环并查询mysql以输出Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆