如何在php中为select查询中的每个组插入一个空行


How to insert a blank line in php for each group from select query

我正在使用PHP从查询中导出文本文件。选择结果如下

Account   student_no
001        stu_001
           stu_001
001        stu_002
           stu_002

我通过(select * from payment group by account,student_no asc)选择了哪个,所以我的问题是,我需要在导出文本文件的每个组(即001 stu_001、001 stu_002)之间插入一个空行。那么我该如何在PHP中做到这一点呢。

首先按帐户ORDER显示结果,然后按

$previousAccount="";
foreach($records as $record)
{
   $account=$record["account"];
   if($account!=$previousAccount)
  {
   $output.=PHP_EOL;  // or echo or fwrite etc
  }
  $previousAccount=$account;
...
...
...
...
}

假设您在字符串中构建输出,只需在双引号之间添加'n

$output_string = 'This line is followed by a new line'. "'n";
$output_string .= 'And I add another line'. "'n";

使用此选项卡可以定义PhpStorm在重新格式化后在代码中保留和插入的空行的位置和数量。对于每种类型的位置,指定要插入的空行数。结果显示在"预览"窗格中。。。。代码样式。PHP

 $output = sprintf( "%20s %20s 'n", "Account","student_no" );    // Print Account   student_no
 foreach($record as $row){
      $output .= sprintf( "%20s %20s",$row['account'], $row['student_no'] ) . "'n";
 }

http://pastebin.com/qrBZzywh