CSV2Table

© 2001, by Sivaprasad R.L <rlsp@netlogger.net>,
http://netlogger.net

An easy to use PHP class to display a CSV (Comma Separated Values) file in
the form of an HTML table.


Methods:

  • set_header(val)
  • Valid parameter values : "OFF" / "ON"

    Use this function to customize the appearence of the top row. To remove header highlighting and to display the top row exactly same as the others call: set_header("OFF").The default value is set to "ON".

  • set_headercolor($col1,$col2)
  • Function to set the foreground and background color of the topmost row. For eg : to set the topmost row in black background with text in white :
        set_titlecolor("#000000","$ffffff");

  • set_bgcolor($col1,$col2)
  • Function to set the background color of alternating rows. Both colors will be used as background colors alternatively.
    For eg : to display red and blue as alternating background colors for the rows in the table:
        set_bgcolor("#ff0000","$0000ff");

  • set_fontcolor($col1,$col2)
  • Function to set the font colors of the text in alternating rows. For eg : to display black and white as alternating font colors for the rows in the table:
        set_fontcolor("#000000","$ffffff");

  • set_tablewidth($width)
  • Function to set the table width in percentile. For eg : to display a table with width= 50%:
        set_tablewidth("50%");

  • showtable(csvfilename,tabletitle,columnstoshow="",filter="")
  • This is the main method used to display a csvfile as an HTML table. The last two parameters 'columnstoshow' and 'filter' are optional. By default, when last two parameters are omitted, all columns and rows in the csv file are displayed.

    Parameters:
    'csvfilename' - The name of the csvfile with the extension .csv.

    'tabletitle' - A string to display the title.

    'columnstoshow' - A string which contains the list of column numbers (separated by ',') in the csv file that are to be displayed. By default, all fields in the csv file are shown. This optional parameter is passed only to display chosen columns from a csv file,

    'filter' - An array of filters (conditions to check) to apply before displaying a row. This optional parameter is defined as follows..
    $filter[fieldnum] = "<operator>,<value>";
    The 'fieldnum' is the column number in csvfile. An operator is the operation applied to the data and value. It can take any of the following logical comparison operators '<' , '>' , '!=' , '=' . The 'value' specfied here next to the operator will be compared to the actual data from the corresponding column (specified using fieldnum) in csv file. If the filter validates to true, the row is displayed, or otherwise the current row will be skipped and will move on to the next row.

    For example: If we want to display a row, only when the value of 'age' exceeds 25 and if the 'age' column number in csv file is '1', then the filter should be..
    
             Operator       Value
                   \        /
                    \      /
    
        $filter[1] = ">, 25";
                |
                |
                o----> Column number in CSV file
    

    Example 1:

    To display the csv file
    <?
        include "csv2table.inc";
        $csv = new csv2table();
        $csv->showtable("customer.csv","Customer Details");
    ?>
    

    Example 2:

    To display 1st and 3rd column of the csv file with two filters.
    <?
        include "csv2table.inc";
    
        // if req, you can pass the path where all your csv files are located as a parameter
        // in the constructor. (note: there is a trailing slash after the directory name)
    
        $csv = new csv2table("csv/");
    
        $filter[1] = ">,20";
        $filter[3] = "!=,joe";
    
        $csv->set_header("OFF");
        $csv->set_tablewidth("50%");
        $csv->showtable("customer.csv","Customer Details","1,3",$filter);
    ?>