3
I’m having trouble visualizing a CSV file. My intention is that I can use the Excel command "data to column" and stay row by row, but the file has skipped line impossible. The code was made based on another example right here posted by an Adm.
date_default_timezone_set('America/Sao_Paulo');
ini_set('memory_limit', '12000M');
require_once($_SERVER['DOCUMENT_ROOT']."/administrator/lib/connection.php");
//echo "<script type=\"text/javascript\"> window.open(\"http://www.tometoo.com.br/generateCSV2.php\", \"_blank\")</script>";
$PDO = Database::Connect();
$SQL = "SELECT idpost, idcomment, cm_id_author, pg_name, ps_date, cm_date, ps_message, cm_name_author, cm_message, pss_likes, pss_shares, pss_comments, cms_like, cms_reply
from fb_post
INNER JOIN fb_pssummary ON pss_idpost = idpost
INNER JOIN fb_page ON ps_idpage = idpage
INNER JOIN fb_comment ON cm_idpost = idpost
INNER JOIN fb_cmsummary ON cms_idcomment = idcomment
where ps_date between '2016-09-01 00:00' and '2016-09-01 23:59:59'
ORDER BY ps_date ASC";
$SQL = $PDO->prepare($SQL);
$SQL->execute();
$result = $SQL->fetchAll(PDO::FETCH_ASSOC);
function array_para_csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function cabecalho_download_csv($filename) {
// desabilitar cache
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// forçar download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposição do texto / codificação
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
cabecalho_download_csv("setembro_1_1M_1_parte" . date("Y-m-d") . ".csv");
echo array_para_csv($result);
the output has been like this in excel 229151370439618_1206843959337016,1206843959337016_1206850609336351,1063115217101837,"2016-09-01 00:15:31","2016-09-01 00:27:42","POLICE VIOLENCE The center of Sà Paulo became a real war square tonight, from the time that Military Police, decided to disperse the demonstration§It is against the coup from the middle of it. The effect, was the dispersion of protesters to various corners and a real hunting§promoted by the police the basis of bombs and rubber bullets. According to information§Ãµes, a young woman is at this time in the Clan-nica Hospital with the strong possibility of having lost sight of an eye and a photograph had his work equipment broken and was arrested. At the time, not even the photograph lawyers were informed about the reason for the arrest§ Pictures by Tadeu Amaral
Coup #Impeachment","Marco Antonio Camelo","Capital letter defends bandits." ,418,76,263,4,0
please ignore facebook content that has nothing to do with my opinion. As you can see beyond the accentuation has the problem of lines.
What separation factor is used in the file
.csv
? I use stitch-and-span.– William Aparecido Brandino
This is one of the parts of my doubt, how could I put the delimiter , but the strange thing is he jump line, not create a single line by while return.
– Christopher Tavares
was placed a
\r\n
at the end of each line, but if you don’t know the separation factor, you won’t know how to correctly divide the text.– William Aparecido Brandino
I did not put r n or PHP_EOL because this PHP function seems to do this, the delimiter you are using is the comma
– Christopher Tavares
The separators, when it has text, more recommended are semicolon and pipe. Not to mention that one is choosing the right form of opening in the spreadsheet itself.
– William Aparecido Brandino
Take a look at this post, I reply how to read/record and view csv http://answall.com/questions/118619/importa-analy-e-extrair-dataos-de-um-csv-com-php/118629#118629
– Gabriel Rodrigues
Look, I used this class once to create a file in excel https://github.com/PHPOffice/PHPExcel based on this example : http://www.voltsdigital.com.br/labs/gerando-planilhas-excel-comphp/
– Andrei Coelho
Use the phpexcel library. And stop suffering.
– Ivan Ferrer
Thank you all, and I will stop suffering! hahaha Applying the library here in the project.
– Christopher Tavares