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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度