We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6048
    • 27 Posts
    Very quick-n-dirty throw-up, no errors handling etc, assumes equal numbers of cells in all rows.

    How do you think, what is the best way to fix non-equal number of cells ? New table ? Automagical COLSPAN ?

    #
    # [[CSVfile? &CSVf=/dir/file.csv]]
    #
    
    if (file_exists($this->config['base_path'].$CSVf)) 
      {$csvlines = file($this->config['base_path'].$CSVf); } 
      else return "Not found $this->config['base_path'].$CSVf" ; 
    
    $csvout = '<table border=1 cellspacing=0 cellpadding=3>';
    foreach ( $csvlines as $line ) 
     {
      $csv=explode(';',$line); $line='<tr><td>'.implode('</td><td>',$csv).'</td></tr>';
      $csvout=$csvout.$line;
     }
    
    return $csvout.'</table>';
      • 32963
      • 1,732 Posts
      Very nice snippet smiley A cool way to easily go from csv to html table.

      This might come in handy for those who want to export information from excel to the web
        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 6048
        • 27 Posts
        Quote from: xwisdom at Nov 17, 2005, 03:07 PM

        Very nice snippet smiley A cool way to easily go from csv to html table.

        Thanx, but it’s not so cool coz if CSV lines have different number of fields, table turns out broken.

        And i see no elegant way to fix it, i.e. insert COLSPAN for repeating ’;’ or missing tail of line.

        The only way i see is to add two ugly nested FOR loops, taking each $csv element and looking ahead for empty elements ....