Revision [4598]

This is an old revision of ShowCsv made by JavaWoman on 2005-01-13 15:47:16.

 

Showing the content of a csv-file


Last edited by JavaWoman:
comment on Christian's Java proposal
Thu, 13 Jan 2005 15:47 UTC [diff]


if you save this as actions/showcsv.php, you have the usage:

{{showcsv file="uploads/file.csv" seperator="," header="on"}}

- file is the name of the file (needs to be on the same server as the wikka?)
- seperator is the seperator for the entries
- if you set header="on", the first entry in the file will be printed strong

Bugs

At the moment, there is a bug if you use a , as seperator or in your file. the explode of the entries on a line won't work. I have no clue why. You can see it here.


<?php
/**
* Prints a table based on a csv-textfile.
*
* With this action a csv file, which has to be on the same server as the wikki, is read and its content presented in a table.
*
* @author mjwilco at yahoo dot com
* @author Nils Lindenberg (http://wikka.jsnx.com/NilsLindenberg)
* @author Stefan Lindenberg (http://wikka.jsnx.com/NilsLindenberg)
*
* file string mandatory  the name of the file which should be shown
*
* delimeter char optional  the delimeter of the entries in the csv-file. standard is ","
*
* header string optional  if set to "on", the first entry will be used as header. Standard is "off";
*/


//parameters
$filename = $vars['file'];
if ($vars['seperator']) $seperator = $vars['seperator'];
else $seperator = ",";
if ($vars['header']) $header = $vars['header'];
else $header = "off";

if (file_exists($filename))
{
   $id = fopen($filename, "r");
   while ($data = fgetcsv($id, filesize($filename)))
   {
    $table[] = $data; //put each line into its own entry in the $table array
   }
   fclose($id);

   //print the table
   echo "<table border=1>\n";

   //first entry handeld seperate, possible header
   $entry = explode($seperator, $table[0][0]);
   echo "<tr>\n";
   for ($j = 0; $j < count($entry); $j++)
   {
    if ($header == 'on') echo "<th>";
    else echo "<td>";
    echo $entry[$j];
    if ($header == 'on') echo "</th>\n";
    else echo "</td>\n";
   }
   echo "</tr>\n";

   for($i=1;$i<count($table);$i++)
   {
    $entry = explode($seperator, $table[$i][0]);
    for ($j=0; $j < count($entry); $j++)
    {
     echo "<td>";
     echo $entry[$j];
     echo "</td>\n";
    }
    echo "</tr>\n";
   }
   echo "</table>\n";
}
else print("File not found.");
?>


Alternate Java based solution

It is a great idea to be able to read csv files. I would like to propose an alternative re-using some Java code I built years ago.
The concept is just a bit different as the input file has to be in this form:
"Title for column 1","Title for column 2",...
"Row 1 data 1","Row 1 data 2",...
"Row 2 data 1","Row 2 data 2",...
 

This allows to have some data with the separator inside the data not being split and considered as two data.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import netscape.javascript.JSObject;
import java.net.URL;

public class CsvReader extends Applet {

 private JSObject win;
 private int number_hidden;
 private int number_input;
 private String[] args =new String[258];
 private Hashtable hiddens= new Hashtable();
 private Hashtable inputs= new Hashtable();
 private String[] result = new String[1];

/* this method parses one data line from the input file and generates  HTML code. Some elements may have to be hidden or have to be inputable depending on the parameters used*/

private String parseLign(String value)
{
String table;
table="<TR>";
int compt=0;
int i;
String temp="";
 while((i=value.indexOf(",\""))!=-1)
      {
      temp = value.substring(1,i-1);
      value = value.substring(i+1);
      if (hiddens.containsKey(args[compt]))
        {
        compt++;
        continue;
        }
      if (inputs.containsKey(args[compt])) table+="<TD> <INPUT type=text size=8 value="+temp+" ></TD>";
      else table+="<TD>"+temp+"</TD>";
      compt++;
      }
  if (!hiddens.containsKey(args[compt]))
    {

    if (inputs.containsKey(args[compt])) table+="<TD> <INPUT type=text size=8 value="+value.substring(1,value.length()-1)+" ></TD>";
    else table+="<TD>"+value.substring(1,value.length()-1)+"</TD>";
    }
  table+="</TR>";
    return table;
 }


 /* this method parses the 1st data line (considered as titles) as generates a string table that contains all titles for all columns */

  private void parseArgs(String str) {
    int compt =0;
    int i;
    String temp;
    while((i=str.indexOf(",\""))!=-1)
      {
      temp = str.substring(1,i-1);
      str= str.substring(i+1);
      args[compt] = temp;
      compt++;
      }
    args[compt]=str.substring(1,str.length()-1);


  }


  /* this method uses the table of the column titles (built here above)  and the input parameters (elements to be hidden, elements to be input) stocked as hashtable to generate the HTML code for the titles*/

  private String parseTitle() {
    String str= "<TABLE border=\"2\" bordercolor=\"#0033CC\" bgcolor=\"#999999\"><TR>";
    int i =0;
    while (args[i]!=null)
      {
        if (hiddens.containsKey(args[i])) {i++;continue;}
        str += "<TD>"+args[i]+"</TD>";
        i++;
      }
    str+= "</TR>";
    return str;
  }

 //Applet initialisation
  public void init() {
    try
      {
      win = JSObject.getWindow(this);
      number_hidden=new Integer(getParameter("number_hidden")).intValue();
      number_input=new Integer(getParameter("number_input")).intValue();
      for (int i=0;i<number_hidden;i++)
        {
        hiddens.put(getParameter("hidden"+i),"hidden");
        }
      for (int i=0;i<number_input;i++)
        {
        inputs.put(getParameter("input"+i),"input");
        }
      URL url = new URL(getCodeBase(),getParameter("datafile"));
      BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
      parseArgs(br.readLine());
      String temp;
      result[0]= parseTitle();
      while ((temp=br.readLine())!=null)
        {
         result[0] += parseLign(temp);
        }
        result[0] += "</TABLE>";
        win.call("display",result);
      }
   catch (Exception e)
    {showStatus("Error during initialisation");}


  }

}


Finally your HTML code should look like this:

<SCRIPT LANGUAGE="JavaScript">
function display(str){document.body.insertAdjacentHTML('beforeEnd',str);}
</SCRIPT>
</HEAD>
<BODY>
<APPLET
CODE = "CsvReader.class"
WIDTH = 1
HEIGHT = 1
HSPACE = 0
VSPACE = 0
ALIGN = center
MAYSCRIPT
>
<PARAM name=datafile value=data/MyFile.csv>
<PARAM name=number_hidden value=2>
<PARAM name=hidden0 value=TitleX>
<PARAM name=hidden1 value=TitleY>
<PARAM name=number_input value=3>
<PARAM name=input0 value=TitleA>
<PARAM name=input1 value=TitleB>
<PARAM name=input2 value=TitleC>

</APPLET>


As you can see, there is one line of javascript called by the java code (win.call("display",result);) to display the html code in your page.

How to use it?

The input file has to be formatted as explained above (in fact a csv file with all data inside double quotes and the first row being the column titles).
The applet needs a few parameters:

To Do

Test it intensively,
Decide what to do when input have been made in the input fields (replace the input file?),
Get the comments of a java expert: looking at the user nicknames in this wiki we should find some ;-)

There are 9 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki