CSV IMPORT
ini_set('auto_detect_line_endings',1);
$handle = fopen('city.csv', 'r');
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
FIELDVALUE1 = str_replace("`","", mysql_real_escape_string($data[0]));
FIELDVALUE2 = str_replace("`","", mysql_real_escape_string($data[1]));
mysql_query("INSERT INTO `TABLE_NAME` (`FIELDNAME1`,`FIELDNAME2`) VALUES ('FIELDVALUE1','FIELDVALUE2')");
$result = (mysql_insert_id()> 0) ? 'Pass' : 'Fail' ;
$output[$result]++;
}
CSV EXPORT
$filename = 'test.csv';
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$out = "";
$mystr = "";
$out .= '"FIELDNAME1";"FIELDNAME2";"FIELDNAME3";"FIELDNAME4"';
$out .= $csv_terminated;
$sql_query = "select * from TABLE_NAME where CONDITION";
$sqlFreetrail = mysql_query($sql_query);
while ($user = mysql_fetch_assoc($sqlFreetrail))
{
$FIELDVALUE1 = $user["FIELDNAME1"];
$FIELDVALUE2 = $user["FIELDNAME2"];
$FIELDVALUE3 = $user["FIELDNAME3"];
$FIELDVALUE4 = $user["FIELDNAME4"];
$mystr .= '"'.$FIELDVALUE1.'";"'.$FIELDVALUE2.'";"'.$FIELDVALUE3.'";"'.$FIELDVALUE4.'"';
$mystr .= $csv_terminated;
}
$out = $out.$mystr;
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
No comments:
Post a Comment