PHP CSV diff script - php

I am currently writing a simple PHP script/site to save the differences of 2 csv files in a 3rd one.
Currently my only problem is, that I want to ignore a specific row (the customer id) which changes from time to time.
I am writing a newsletter system and I have 2 csv files with customer data, and I am checking, if the email address has changed since the last csv file.
How can I ignore the customer id, I can't just delete the column, because I need that column for later.
Here is my structure:
CSV file 1 (old database):
customerid,sex,name,firstname,zip,email
1,male,smith,will,1234,will.smith#gmail.com
2,male,doe,john,7367,john#doe.com
3,female,doe,anna,7367,anne#doe.com
CSV file 2 (new database):
customerid,sex,name,firstname,zip,email
1,male,smith,will,2224,will.smith#gmail.com
7,male,doe,john,7367,john#gmail.com
20,female,doe,anna,7367,anne#doe.com
As you can see, in the newer file, the customer id for John and Anna Doe changed, this should be ignored.
What shouldn't be ignored, is that the email for John Doe and the zip for Will Smith changed. The second "step" works perfectly.
Here is my full code: http://pastebin.com/bt7Pj3MP (about 30 lines). There are the essential parts:
$file1 = file('2015-07-01.csv', FILE_IGNORE_NEW_LINES);
$file2 = file('2015-07-09.csv', FILE_IGNORE_NEW_LINES);
sort($file1);
sort($file2);
$diff = array_diff($file2, $file1);
array_unshift($diff, $_POST['csv_cols']);
$output = substr(md5(rand()), 0, 5). "-output.csv";
file_put_contents($output, implode(PHP_EOL, $diff));
unlink($file1_name);
unlink($file2_name);
header('Content-Description: Download ' . $output);
header('Content-Type: application/force-download');
header("Content-Type: application/download");
header("Content-Length: " . filesize($output));
header("Content-disposition: attachment; filename=\"" . basename($output) . "\"");
readfile($output);
unlink($output);
exit;

Related

export mysql tables and zipArchive

I've searched all the posts, but still couldn't get it to work. With button press I want to export "page preset" to be precise it's restaurant menu preset, it includes css files, mysql tables etc. I want to be able to import it to another 'menu'. First I'm trying to export mysql database. Should I use mysqldump or SELECT * INTO OUTFILE ?
I'm using this line:
exec("mysqldump --user=$dbusername --password=$dbpassword restaurantsdb meal --where=restaurant_id=$restId > tables/meal.sql");
restaurantsdb is database name and meal = table name. I also want rows WHERE 'restaurant_id' = {id}
I'm trying to understand how mysqldump works, should this line work for me?
Next I'm creating zipArchive file, adding some directories, I tried adding .txt file, that one works, but it doesn't seem to find meal.sql file.
$zip = new ZipArchive();
if ( $zip->open($zip_file, ZipArchive::CREATE) !== TRUE) {
exit("error");
}
$zip->addEmptyDir('TestFiles');
$zip->addEmptyDir('tables');
$fileToZip = __DIR__.'/hello.txt';
$zip->addFile($fileToZip, "TestFiles/text.txt");
$fileToZip = __DIR__.'/tables/meal.sql';
$zip->addFile($fileToZip, "tables/meal.sql");
$download_file = file_get_contents( $file_url );
$zip->addFromString(basename($file_url),$download_file);
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zip_file).'"');
header("Content-length: " . filesize($zip_file));
header("Pragma: no-cache");
header("Expires: 0");
ob_clean();
flush();
readfile($zip_file);
unlink($zip_file);
exit;
Also, is it better to export as .sql or .csv ? Later when importing, I need to be able to change id's of all rows to specified, just before importing to database. It's basically cloning same data, but different id's.

PHP export array data into column of data in CSV

I'm trying to export nationality data into CSV. My nationality data are stored in an array. However, when i export it out, my CSV output show this error "Warning: fputcsv() expects parameter 2 to be array". Can I know how i can show my nationality data in a csv column?
my code:
$filename = "nationality_list.csv"; // Create file name
$array_nat = array('BANGLADESH','CAMBODIA','CHINESE','DUTCH','FILIPINO','FRENCH','GERMAN','INDIAN','INDONESIA','ITALY','JAPANESE','KOREAN','LITHUANIAN','MALAYSIA','MYANMAR','NEW ZEALAND','RUSSIAN','SINGAPOREAN','SRI LANKAN','THAILAND','UK','USA');
$f = fopen('php://memory', 'w');
$fields = array('Nationality_list');
fputcsv($f, $fields);
foreach($array_nat as $nat){
fputcsv($f, $nat);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f)
Desired output in CSV
Try this:
fputcsv($f, [$nat]);
fputcsv expects an array because a row consists of n columns. 1 in your case :)

mysqli export to xls issue

I have troble geting this work and I can't figure it out what is the issue.
I download a xls file, but it doesent opens.
I had a mysql script like this, working, and I tried to convert it into mysqli and probably something is wrong...
Thanks in advance
$sqlExp = "SELECT * FROM table";
$countQryExp = mysqli_query($link, $sqlExp );
$filename = "sampledata.xls"; // File Name
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
while($row=mysqli_fetch_array($countQryExp,MYSQLI_ASSOC))
{
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
echo implode("\t", array_values($row)) . "\r\n";
}
There's a lot more to generating an Excel file than giving it a content type of application/vnd.ms-excel. Excel is a very particular format, whereas you're generating a TSV file - tab separated values, and in a pretty breakable manner (what happens if someone puts a \t in one of your site's fields, or a new line?).
If you want to generate real Excel files, you'll want one of the various libraries for doing so. If a CSV/TSV are fine, just export a .csv/.tsv file with proper headers.

PHP: Creating csv file if data text contains "," data will be splitted into parts and put it in other column?

I got problems in creating a csv file because if my data contains , it will be splitted into parts and put it in other columns that destroys my csv file.
Here is the sample output:
Sample output link
As what you can see the first row in TITLE column should have this data "Acer Aspire AS5250-0639 15.5" Laptop (1.65 GHz AMD Dual-Core Processor E-450, 4 GB RAM, 500 GB Hard Drive, DVD+/-RW Optical Drive, Windows 7 Home Premium 64-bit)" starting in 4 GB RAM it is splitted and put it in other columns because before it it has , and more.
As of now here is my PHP script:
<?php
include 'database.php';
$db = new database();
$data = $db->exportdatabase();
$out = '';
$out .= 'GSEQ,BRAND,TITLE,SIMAGE,SPRICE,WEIG,DIMENSIONS,ASIN,CATEGORYNAME,MODELNO';
$out .="\n";
foreach($data as $items){
$out .=' '.$items['GSEQ'].', '.$items['BRAND'].', '.$items['TITLE'].', '.$items['SIMAGE'].', '.$items['SPRICE'].', '.$items['WEIG'].', '.$items['DIMENSIONS'].'
'.$items['ASIN'].', '.$items['CATEGORYNAME'].', '.$items['MODELNO'].'
';
$out .="\n";
//var_dump($items);
}
header('Content-Description: File Transfer');
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename=Data.csv");
header("Expires: 0");
echo $out;
exit;
?>
How can I possibly fix this issue?
For starters I would stop trying to do something yourself that PHP can do for you. Use fputcsv instead of doing it yourself: http://php.net/manual/en/function.fputcsv.php. This method also deals with escaping your delimiter ( your comma ).
On top of that.. dont use ',' as your csv seperator. You are probably better off using '|' as a seperater, since that character is way less common.
One method is to wrap each field using quotes (")
Another is to replace the troublesome "," with another character using str_replace.

place file into zip and stream to browser - zero physical files

basically we currently generate a csv file and push it too the clients browser to download, so no physical files are involved or stored as these are reports that always change.
These have become quite large in some places and I need to reduce the speed and size of the downloads to clients.
I decided to go the zip root and got the below test script working fine, however i am still having to generate the zip file physically and then later delete it, obviously there is a speed factor here as I am writing and reading the file, when I only need to push the zip files contents to the browser.
is there no way to create a stream with the ziplib?
<?php
//report
$reportFilename = 'report';
$data = '005,"756607 ","WED","L","TEST UITENHAGE CBD F NFD"
005,"756608 ","MON","L","TEST SUMMERSTRAND NNB "
005,"756634 ","WED","L","TEST UITENHAGE PENFORD F"
005,"756776 ","MON","L","TEST WALKER DRIVE FOOD "
005,"756858 ","MON","C","TEST R ADAMI&SONS C C "
005,"801002 ","MON","L","TESTMOFFET NNB "
005,"CP00270 ","WED","L","TEST WALMER P FNF "
'; //dummy data
// populate fake data...
$reportData = $data.$data.$data.$data.$data.$data.$data.$data.$data.$data.$data;
//make unique so no clashing occurs
$zipFilename = '_temp_'.microtime().$zipFilename;
//zip data
$zip = new ZipArchive();
$zip->open($zipFilename, ZIPARCHIVE::CREATE);
$zip->addFromString($reportFilename.'.csv' , $reportData); //add report
$zip->close();
$zipData = file_get_contents($zipFilename);
$zipSize = filesize($zipFilename);
unlink($zipFilename);
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"".$reportFilename.".zip\"");
header('Content-Transfer-Encoding: binary');
header("Content-Type: application/zip");
echo $zipData;
?>

Categories