PHP Classes

File: csv_read.php

Recommend this page to a friend!
  Classes of Mark Berube   csv2tbl   csv_read.php   Download  
File: csv_read.php
Role: Example script
Content type: text/plain
Description: Working example of usage
Class: csv2tbl
Display the contents of a CSV file in a HTML table
Author: By
Last change: updated to show the new way of refering to columns by letter, like spreadsheets do. Also shows how to template a columns output to create hyperlinks or call scripts.
Date: 10 years ago
Size: 3,395 bytes


Class file image Download

$f = (isset($_GET['f'])) ? $_GET['f'] : 'csv.txt';
$tbl = new csv2tbl($f,'|');
    // If desired, you could feed your own col headers to the class rather than using the first row fo the csv
    //$tbl -> set_custom_headers(array('Operator','Date','Act','Stat','Msg'));
$tbl -> set_first_line_as_headers(true);//optional
    //$tbl -> show_headers(true); //setting custom headers (or setting first_line_as_headers) would make this call un-needed, if no other headers were set, this method would create generic ones
$tbl -> set_border(0);
$tbl -> set_cellpadding(3);
$tbl -> set_table_id('CSVTbl_1');
$tbl -> set_table_class('CSVTable');
$tbl -> show_line_nums(true); //do you want to prepend the rows with line numbers?
$tbl -> set_skip_columns(array('D')); //we will not be displaying the 4th column (...or the 'asset_class' column in this example). this class views the first col as 'A' not 0 or 1
if(isset($_GET['show_col']) && isset($_GET['show_val'])){
$show_col = $_GET['show_col'];//assumes 'A' is the first column
$show_val = $_GET['show_val'];
$tbl -> show_only_where(array($show_col=>$show_val));
//we want to hyperlink the column 'E' value
$tbl -> set_template_cols_array(array('E'=>"<a href=\"javascript:void(alert('CSV Cell - E:[RowNum]'))\">[E]</a>"));
$content = $tbl->show_table();
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

    <title>View CSV</title>
    <script language="Javascript" type="text/javascript" src="js/sorttable.js"></script>
        #navbar a:link{color: #ad0000;text-decoration:none; font-family:arial, helvetica, sans-serif, verdanna; font-size:10pt; margin:0px;}
        #navbar a:visited {color: #ad0000;text-decoration:none; font-family:arial, helvetica, sans-serif, verdanna; font-size:10pt; margin:0px;}
        #navbar a:hover {color: #000000; text-decoration: underline; font-size:10pt; margin:0px;}
echo isset($_GET['show_val']) ? '#'.$_GET['show_val'].' {background-color: #e0e0e0;}'."\n":'';
#csvtbl{font-family: arial,helvetica,verdana,sans-serif; font-size:10pt;}
<div id="container">
    <h3>Viewing Records (Table created from CSV)</h3>
    <div id="navbar">
        //we will create links to display only certain results:
        //These link paramaters will be used the the show_only_where() method to compare column number 6 to the desired value
        //Column 6 is the manuf column in the default the manuf column WOULD be 7, but in this case it is 6 since we choose
        //not to display the 'asset_class' column in this example
$log_types = array('FORD' => 'Show only FORD','CHEVY' => 'Show Only Chevy');
$search_col = 'G';
$log_types as $type => $display){
"<a id=\"$type\" href=\"".basename(__FILE__)."?show_col=$search_col&show_val=$type\">$display</a> | \n";

echo $content;