Hi Guys,
I found the below snippet that parses CSV’s and outputs a table.
It works fine, but I want to simplify it so that i can enter a string that is the file name in the CSV file, and based on that string it will display a view count.
see an example csv at Thailand.TV/ttv.csv
So my resulting snippet call would be
[[csv2table? &csv=`/ttv.csv` &url=`http://thailand.tv/assets/videos/hQHostel/hq.flv`]]
And the resulting output would simply be 14
Any guidance appreciated.
Stuart
<?php
# csv2table 0.1 - Converts CSV to HTML table
# Created by Chris Pratt 09/08/09.
# -----------------------------------------------------
# _Version_History_
# 0.1 - Inital Release
#
# _Basic_Usage_
# This snippet will only return the content of the table and not the
# opening and closing <table> tags. This is by design to allow you to
# add any attributes, classes or id to the table at will.
#
# Ex.
#
# <table>
# [[csv2table? &csv=`/path/to/file.csv`]]
# </table>
#
# _Parameters_
# &csv : Path to CSV file (relative to document root)
# &delimiter : Character that separates values (defaults to `,`)
# &enclosure : Character that encloses strings (defaults to `"`)
# &hasColHeader : First row is column headings (boolean 0 or 1, defaults to 1)
# &hasRowHeader : First column of each row is a header (boolean 0 or 1, defaults to 0)
# &alternateRows : Add a class to every 2nd row (boolean 0 or 1, defaults to 1)
# &altRowClass : Classname to use for alternating rows (defaults to `alt`)
#
# _Notes_
# MODx snippets can be called with either `[[...]]` or `[!...!]`. The first
# is a "cached" snippet call: the results of the snippet will be saved and
# used again later instead of re-running the snippet. The second form is an
# "uncached" snippet call: the results will not be saved and the snippet will
# be run each time. Processing CSV files, especially large CSV files, may
# consume an inordinant amount of resources, so you should always used the
# "cached" version (`[[...]]`) of the snippet call to save processing time.
$params = array(
'csv' => isset($csv) ? $csv : '',
'delimiter' => isset($delimiter) ? $delimiter : ',',
'enclosure' => isset($enclosure) ? $enclosure : '"',
'hasColHeader' => isset($hasColHeader) ? $hasColHeader : 1,
'hasRowHeader' => isset($hasRowHeader) ? $hasRowHeader : 0,
'alternateRows' => isset($alternateRows) ? $alternateRows : 1,
'altRowClass' => isset($altRowClass) ? $altRowClass : 'alt'
);
class Csv2table {
var $params;
var $fp;
function Csv2table (&$params) {
//destructor
register_shutdown_function(array(&$this, ‘__destruct’));
//constructor
$argcv = func_get_args();
call_user_func_array(array(&$this, ‘__construct’), $argcv);
}
function __construct(&$params) {
$this->params = $params;
} // __construct
function __destruct() {
if ($this->fp) fclose($this->fp);
unset($this->params);
unset($this->fp);
} // __destruct
function load_csv_file () {
if (isset($this->params['csv'])) {
if (substr($this->params['csv'],0,1) != '/') $this->params['csv'] = '/'.$this->params['csv'];
if (file_exists("{$_SERVER['DOCUMENT_ROOT']}{$this->params['csv']}")) {
$this->fp = fopen("{$_SERVER['DOCUMENT_ROOT']}{$this->params['csv']}", 'rb');
} else {
return 'Could not find CSV file!';
}
} else {
return 'No CSV file specified!';
}
if (!$this->fp) {
return 'Could not open CSV file!';
}
return false;
} // load_csv_file
function build_table () {
$output = '';
if ($this->params['hasColHeader']) {
$row = fgetcsv($this->fp, 1024, $this->params['delimiter'], $this->params['enclosure']);
$output .= ' <tr class="heading">'."\n";
foreach($row as $v) {
$v = ($v == '') ? ' ' : htmlentities($v);
$output .= ' <td>'.$v.'</td>'."\n";
}
$output .= ' </tr>'."\n";
}
$row_count = 0;
while ($row = fgetcsv($this->fp, 1024, $this->params['delimiter'], $this->params['enclosure'])) {
$row_count++;
$row_class = ($this->params['alternateRows'] && (($row_count % 2) == 0)) ? ' class="'.$this->params['altRowClass'].'"' : '';
$output .= ' <tr'.$row_class.'>'."\n";
foreach($row as $k => $v) {
$v = ($v == '') ? ' ' : htmlentities($v);
if (($k == 0) && $this->params['hasRowHeader']) {
$output .= ' <th>'.$v.'</th>'."\n";
} else {
$output .= ' <td class="norm">'.$v.'</td>'."\n";
}
}
$output .= ' </tr>'."\n";
}
if ($this->params['hasColHeader']) {
}
return $output;
} // build_table
function get_output () {
if (!$this->fp) $errors = $this->load_csv_file();
if ($errors) return $errors;
elseif (!$this->fp) return 'Could not open CSV file!';
else return $this->build_table();
} // get_output
}
$csv2table = new Csv2table($params);
return $csv2table->get_output();