du21064 2012-10-31 19:55
浏览 63
已采纳

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);

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
}

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

//resume normal processing

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

// 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;
  • 写回答

1条回答 默认 最新

  • dongxie1907 2012-10-31 20:29
    关注

    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]*$/
    

    But form your comments

    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

    All you need is

    $string = "111X222X3333" ;
    list($sid,$gid,$qid) = explode("X", $string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧