我有一个要求,我将每个页面访问和活动记录到csv日志文件中。我该怎么写才能首次写入标题,而不是每次都写。
fputcsv($fh1, array(date,id1,page,id2,request,ip));
fputcsv($fh1, array($date,$mid,$page,$aid,$request,$ip));
第一个数组只应为特定的日期日志文件写入一次。请建议。
答案 0 :(得分:0)
我希望您在下载CSV时尝试使用标题排列记录。如果是,那么请看下面这个例子,我在小例子中显示它在Drupal中工作 -
<?php
function getUsersDetails(){
$csv_output = '';
$all_users_entry = db_query("SELECT * FROM users");
while($users = db_fetch_object($all_users_entry)){//suppose these fname,lname etc are column name of your table.
$fname = $users->fname;
$lname = $users->lname;
$date = $users->joining_date;
$role = $users->role;
$location = $users->location;
$csv_output .= "\"".$fname."\"".","."\"".$lname ."\"".","."\"".$date."\"".","."\"".$role."\"".","."\"".$location."\""."\r";
}
return $csv_output;
}
function download_details(){
$csv_output = '';
$csv_output .= "First Name,Last name,Member Since,Role,Place\n";
$csv_output .= getUsersDetails();
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="Usera_Details.csv"');
print $csv_output;
exit;
}
?>
现在您可以从任何地方调用此函数download_details(),您将在下载的CSV中拥有用户表详细信息
答案 1 :(得分:0)
当你看到这个时,你会踢自己......
if (!file_exists('log.csv'))
{
// Make the file & Write the header
}
// Write the log entry.