diff options
author | Silvio <silvio@devlet.com.br> | 2010-06-09 11:39:35 -0300 |
---|---|---|
committer | Silvio <silvio@devlet.com.br> | 2010-06-09 11:39:35 -0300 |
commit | 46f3f4e07ffa15bb5b563462aca530ba360d341b (patch) | |
tree | 790d6b572c22db011b1dc9adb06b9a5c80fb65b8 | |
parent | 3a466ad7fc6a95ff44344ed808885967cd1e6ee3 (diff) | |
download | cinisis-46f3f4e07ffa15bb5b563462aca530ba360d341b.tar.gz cinisis-46f3f4e07ffa15bb5b563462aca530ba360d341b.tar.bz2 |
Merging subfields on csv export
-rw-r--r-- | tests/csv.php | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/tests/csv.php b/tests/csv.php index c48118b..c08b18f 100644 --- a/tests/csv.php +++ b/tests/csv.php @@ -31,6 +31,28 @@ function filter(&$field = NULL) { $field = str_replace('>', '', $field); } +/** + * Merge fields in a single cel. + * + * @param $data + * Array with field data. + * + * @param $field + * Field name. + * + * @return + * Cel with merged fields. + */ +function merge_fields($data, $field) { + $cel = ''; + $sep = (count($data) > 1) ? '; ': ''; + foreach ($data as $subkey => $subvalue) { + $cel = $cel . $data[$subkey][$field] . $sep; + } + + return $cel; +} + // Import Cinisis Library. require_once '../index.php'; @@ -44,12 +66,10 @@ if ($isis->db) { $format = $isis->db->format; // Prepare output. - /* header("Content-type: application/text/x-csv"); header("Content-Disposition: attachment; filename=export.csv"); header("Pragma: no-cache"); header("Expires: 0"); - */ // Format fields. foreach ($format['fields'] as $field) { @@ -65,7 +85,7 @@ if ($isis->db) { echo "\n"; // Format output. - for ($n=1; $n <= 30; $n++) { + for ($n=1; $n <= $rows; $n++) { $result = $isis->db->read($n); if ($result) { @@ -81,12 +101,9 @@ if ($isis->db) { } if (is_array($field['subfields'])) { foreach ($field['subfields'] as $key => $value) { + // Deals with subfield repetition. if (isset($result[$field['name']][0][$value])) { - $cel = ''; - foreach ($result[$field['name']] as $subkey => $subvalue) { - $cel = $cel . $result[$field['name']][$subkey][$value] .'; '; - } - echo csv($cel); + echo csv(merge_fields($result[$field['name']], $value)); } else { echo csv($result[$field['name']][$value]); |