mysql - PHP to CSV - break one row into multiple rows -
i have php code converts db table csv file - works treat.
the db info entered modx (cms) login/user sign system.
this added 1 row, fine need of columns under each other in csv. can order , organise csv when need collate things. example order users state.
here example:
id | memeberno | branch name 1 | branch state 1 | branch country 1 | branch name 2 | branch state 2 | branch country 2 etc.... 15 branches.
i need line break after each branch out puts this:
id | memeberno | branch name 1 | branch state 1 | branch country 1 (line break here???) branch name 2 | branch state 2 | branch country
does make sense? rather have php db dont want mess login system. guess needs line break after columns 21 every 6 columns while adding 20 columns in 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(); ($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); // field name ($i = 0; $i < $columns_total; $i++) { $heading = mysql_field_name($sql, $i); $output .= '"'.$heading.'",'; } $output .="\n"; // records table while ($row = mysql_fetch_array($sql)) { ($i = 0; $i < $columns_total; $i++) { if ($i == 25){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 32){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 39){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 46){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 53){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 60){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 67){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 74){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 81){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 88){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 95){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 102){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 109){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }elseif ($i == 116){ $output .="\n"; $output .=",,,,,,,,,,,,,,,,,,"; $output .='"'.$row["$i"].'",'; }else { $output .='"'.$row["$i"].'",'; } } $output .="\n"; } // download file $filename = "myfile.csv"; header('content-type: application/csv'); header('content-disposition: attachment; filename='.$filename); echo $output; exit; } ?>
Comments
Post a Comment