i have a requirement where i am logging each page visit and activities into csv log file. how can i write so that the header should be written for the first time only not every time.
fputcsv($fh1, array(date,id1,page,id2,request,ip));
fputcsv($fh1, array($date,$mid,$page,$aid,$request,$ip));
The first array should be written once only for a particular date log file. Please Suggest.
You're going to kick yourself when you see this ...
if (!file_exists('log.csv'))
{
// Make the file & Write the header
}
// Write the log entry.
I hope you are trying to arrange the record with header when you download CSV. If YES then please have a look on this example below, where I am showing it in small example which works in 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."\""."";
}
return $csv_output;
}
function download_details(){
$csv_output = '';
$csv_output .= "First Name,Last name,Member Since,Role,Place
";
$csv_output .= getUsersDetails();
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="Usera_Details.csv"');
print $csv_output;
exit;
}
?>
Now you can call this function download_details() from any place and you will have users table details in the downloaded CSV