PHP到CSV - 将一行分成多行

I have some php code which converts a DB table into a csv file - works a treat.

The DB info is entered by a Modx (CMS) login/user sign up system.

This is added into one row, which is fine but I now need some of the columns under each other in the csv. so I can order and organise the csv when I need to collate certain things. For example order all users by their state.

Here is an example:

ID | memeberNo | branch name 1 | branch state 1 | branch country 1 | branch name 2 | branch state 2 | branch country 2 etc.... up to 15 branches.

I need a line break after each branch so it out puts like this:

ID | memeberNo | branch name 1 | branch state 1 | branch country 1 (line break here???)
                 branch name 2 | branch state 2 | branch country

Does that make sense? I would rather have the php do this than the DB as I dont want to mess with the login system. So I guess it needs a line break after columns 21 then every 6 columns while adding 20 columns in so they line up.

$entire = $_POST['entire'];
if ($entire == "yes"){
global $modx;
$delete =$_POST['database'];
$modx->db->delete($delete);
}
if ($entire == "no"){
global $modx;
$delete =$_POST['database'];
$id =$_POST['id'];
$modx->db->delete($delete,"id=$id");
}
if ($entire == "export"){
global $modx;
$delete =$_POST['database'];

$result = $modx->db->select("*", $delete );

if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
     $headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

     fputcsv($fp, array_values($row));

}
die;
}
}
<?php
$entire = $_POST['entire'];
if ($entire == "yes"){
global $modx;
$delete =$_POST['database'];
$modx->db->delete($delete);
}
if ($entire == "no"){
global $modx;
$delete =$_POST['database'];
$id =$_POST['id'];
$modx->db->delete($delete,"id=$id");
}
if ($entire == "export"){
global $modx;
$delete =$_POST['database'];

//$result = $modx->db->select("*", $delete );



$output = "";
$sql = $modx->db->select("*", $delete );
$columns_total = mysql_num_fields($sql);

// Get The Field Name

for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="
";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
if ($i == 25){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 32){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 39){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 46){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 53){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 60){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 67){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 74){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 81){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 88){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 95){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 102){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 109){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 116){
    $output .="
";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}else {
    $output .='"'.$row["$i"].'",';  
}
}
$output .="
";
}

// Download the file

$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;

}
?>