class_mcCsv.inc.php
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class mcCsv {
/**
* transform array to csv format and export as file
*
* @since 20110920 @author n@mensch.coop
* @static
* @downgraded for php4
*
* @param array $param_array
* @param type $param_filename
* @param array $param_search
* @param array $param_replace
*/
function exportArrayToCsv(array $param_array, $param_charset='utf-8', $param_filename='download.csv', array $param_search = array('"', ';'), array $param_replace = array("'", " ")) {
$ausgabe = '';
if (is_array($param_array) && count($param_array)!=0) {
foreach ($param_array as $daten) {
if (is_array($daten) == true) {
foreach ($daten as $key => $value) {
$ausgabe .= '"'.str_replace($param_search, $param_replace, $daten[$key]).'";';
}
}
$ausgabe .= "\n";
}
} else {
$ausgabe .= '"no data;"';
}
if (headers_sent() == false) {
header("Content-type: text/csv; charset='.$param_charset");
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header("Content-Disposition: attachment; filename =".$param_filename);
header('Pragma: no-cache');
}
echo $ausgabe;
}
/**
*
* @param array $param_csvConfig -- array(0=>'col1',1=>'col2',2=>'col3')
* @param array $param_csvValues -- array(0=>'val1',1=>'val2',2=>'val3')
* @return array|boolean
*/
public static function getCsvArrayForWriteDB(array $param_csvConfig, array $param_csvValues) {
if(is_array($param_csvConfig) && !empty($param_csvConfig)) {
$values = array();
foreach($param_csvConfig as $csv_coloum => $tab_cloumn) {
$values[$tab_cloumn] = $param_csvValues[$csv_coloum];
}
return $values;
} else {
return false;
}
}
/**
* show how the format for csv
*
* @param array $param_csvConfig -- array(0=>'col1',1=>'col2',2=>'col3')
*/
public static function showCsvFormatAsHtml(array $param_csvConfig) {
if(!is_null($param_csvConfig)) {
// $csv_columns = mcConfig::getConfig('csv', 'product');
$cvs_import_header = array();
$csv_header = '';
for($i=0; $i<count($param_csvConfig); $i++) {
if($param_csvConfig[$i]) {
$cvs_import_header[] = mcLanguage::getWord($param_csvConfig[$i]);
$csv_header .= '"'.mcLanguage::getWord($param_csvConfig[$i]).'", ';
}
}
mcTableHtml::showTable(array($cvs_import_header), mcLanguage::getWord('rowformat').': '.$csv_header);
}
}
}
?>