0
我寫了一個將mysql數據轉換爲csv文件的代碼。問題是,它顯示的結果水平爲由php水平生成格式化csv文件
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
但是我想它垂直的
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
這裏是我的代碼:
<?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;
}
}
?>
林不知道,即使其可能的,因爲即時通訊新的與csv。如果你們有任何想法和我一起出現,我將不勝感激。謝謝。
thnk you mustafa ...會讓你知道它是否可以.. :) –