I’ve written a code that converts a mysql data into csv file. The problem is that it shows result horizontally as
DATE AUTHOR LIKES COMMENTS` 1/31/2013 WTF Facts 211 3 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 DATE AUTHOR LIKES COMMENTS` 1/31/2013 WTF Facts 211 3 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23
however i want it vertically as
DATE AUTHOR LIKES COMMENTS` DATE AUTHOR LIKES COMMENTS` 1/31/2013 WTF Facts 211 3 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 3 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23 1/31/2013 WTF Facts 211 23
Here is my code:
<?php
if(isset($_POST['submit'])):
set_time_limit(99999999);
ini_set('memory_limit', "9999M");
$filename_csv = "export_".rand(1000000,9999999)."_".rand(1000000,9999999).".csv";
$path = "serp_csvs_page/".$filename_csv;
$fp = fopen($path, 'w');
$page_ids = $_POST['page_id'];
$date_from = $_POST['date_from'];
$date_to = $_POST['date_to'];
foreach($page_ids as $page_id):
$brline = array("\n");
$page_name = get_page_name_by_id($page_id);
$dates = checkCsvQuotes("DATE: FROM {$date_from} TO {$date_to}") ;
$page_name = checkCsvQuotes("PAGE ID: {$page_name['page_id']}");
$header = array($dates, "\n", $page_name);
fputcsv($fp, $header);
fputcsv($fp, $brline);
$query = sprintf("SELECT post_date AS `DATE`, post_from AS `AUTHOR`, message AS `MESSAGE`, total_likes AS `TOTAL LIKES`, total_comments AS `TOTAL COMMENTS` FROM tbl_contents_pages WHERE `pid_id` = '$page_id' AND `post_date` >= '$date_from' AND `post_date` <= '$date_to'");
$result = mysql_query($query, $con) or die(mysql_error($con));
$rows = mysql_fetch_assoc($result);
if ($rows) {
$arr_key = array_keys($rows);
fputcsv($fp, $arr_key);
while($rows = mysql_fetch_assoc($result))
{
$date_row = checkCsvQuotes($rows['DATE']);
$author_row = checkCsvQuotes($rows['AUTHOR']);
$message_row = checkCsvQuotes($rows['MESSAGE']);
$likes_row = checkCsvQuotes($rows['TOTAL LIKES']);
$comments_row = checkCsvQuotes($rows['TOTAL COMMENTS']);
$arr_value = array();
$arr_value[] = $date_row;
$arr_value[] = $author_row;
$arr_value[] = $message_row;
$arr_value[] = $likes_row;
$arr_value[] = $comments_row;
//print_r($arr_value);
fputcsv($fp, $arr_value);
}
}
else
{
$noData = array("DO RECORDS FOUND.");
fputcsv($fp, $noData);
}
fputcsv($fp, $brline);
endforeach;
fclose($fp);
echo "<a href=serp_csvs_page/".$filename_csv." class='btn btn-primary'> View File </a>";
endif;
function get_page_name_by_id($id)
{
$query = mysql_query("SELECT page_id FROM tbl_pages WHERE `id` = '$id'");
$result = mysql_fetch_assoc($query);
return $result;
}
function checkCsvQuotes($string)
{
if (strpos($string,'"') !== false) {
return '"'.str_replace('"','""',$string).'"';
} elseif (strpos($string,',') !== false) {
return '"'.$string.'"';
} else {
return $string;
}
}
?>
Im not sure even if its possible, since im new with csv. I would greatly appreciate if you guys have any idea bout it and sharae with me. thank you.
You need to keep extending the array, not instantly spit the elements out. I’ve changed your code to do so, but as I’m unable to test it, it may very likely not work. Either way, compare the code to get the idea of what needs to be done.