We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    something like that snippet should do it:

    [[!getCsvFileItems? 
    &file=`assets/files/csvtest.csv`
    &columnheadings=`1`
    ]]


    <?php
    /*
    [[!getCsvFileItems? 
    &file=`assets/files/csvtest.csv`
    &columnheadings=`1`
    ]]
    */
    
    $file = $modx->getOption('file', $scriptProperties, '');
    $delimiter = $modx->getOption('delimiter', $scriptProperties, ',');
    $columnheadings = $modx->getOption('columnheadings', $scriptProperties, false);
    $outerTpl = $modx->getOption('outerTpl', $scriptProperties, '@CODE:<table>[[+heading]]<tbody>[[+rows]]</tbody></table>');
    $headingTpl = $modx->getOption('headingTpl', $scriptProperties, '@CODE:<thead><tr>[[+_fields]]</tr></thead>');
    $headingFieldTpl = $modx->getOption('headingFieldTpl', $scriptProperties, '@CODE:<th>[[+_field]]</th>');
    $rowTpl = $modx->getOption('rowTpl', $scriptProperties, '@CODE:<tr>[[+_fields]]</tr>');
    $rowFieldTpl = $modx->getOption('rowFieldTpl', $scriptProperties, '@CODE:<td>[[+_field]]</td>');
    
    if (!function_exists('parse_csv_file')) {
        function parse_csv_file($file, $columnheadings = false, $delimiter = ',', $enclosure = "\"") {
            $row = 1;
            $rows = array();
            $handle = fopen($file, 'r');
    
            while (($data = fgetcsv($handle, 1000, $delimiter, $enclosure)) !== false) {
    
                if (!($columnheadings == false) && ($row == 1)) {
                    $fieldnames = $data;
                } elseif (!($columnheadings == false)) {
                    foreach ($data as $key => $value) {
                        unset($data[$key]);
                        $data[$fieldnames[$key]] = $value;
                    }
                    $rows[] = $data;
                } else {
                    $rows[] = $data;
                }
                $row++;
            }
    
            fclose($handle);
    
            $result['fieldnames'] = $fieldnames;
            $result['rows'] = $rows;
    
    
            return $result;
        }
    }
    
    if (!function_exists('parseCsvGetChunk')) {
        function parseCsvGetChunk($tpl, $properties) {
            global $modx;
            if (substr($tpl, 0, 6) == "@CODE:") {
                $template = substr($tpl, 6);
                $chunk = $modx->newObject('modChunk');
                $chunk->setCacheable(false);
                $chunk->setContent($template);
                $output = $chunk->process($properties);
            } else {
                $output = $modx->getChunk($tpl, $properties);
            }
            return $output;
        }
    }
    
    $file = $modx->getOption('base_path') . $file;
    $result = parse_csv_file($file, $columnheadings, $delimiter);
    
    $output = array();
    $output['heading'] = '';
    
    if ($columnheadings) {
        $heading['_fields'] = '';
        foreach ($result['fieldnames'] as $fieldname) {
            $properties = array();
            $properties['_field'] = $fieldname;
            $heading['_fields'] .= parseCsvGetChunk($headingFieldTpl, $properties);
        }
        $output['heading'] = parseCsvGetChunk($headingTpl, $heading);
    }
    
    $rows = array();
    foreach ($result['rows'] as $row) {
        $row['_fields'] = '';
        foreach ($row as $field) {
            $properties = array();
            $properties['_field'] = $field;
            $row['_fields'] .= parseCsvGetChunk($rowFieldTpl, $properties);
        }
        $rows[] = parseCsvGetChunk($rowTpl, $row);
    }
    
    $output['rows'] = implode('', $rows);
    
    return parseCsvGetChunk($outerTpl, $output);
    
    ?>
    
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 44991
      • 13 Posts
      Thank you So Much Bruno17. smiley smiley smiley smiley smiley

      Thanks Everyone for your Help and Time..
        • 38705
        • 101 Posts
        Quote from: Bruno17 at Sep 04, 2013, 12:38 AM
        with some little tricks it should be possible to have a import csv - button above the MIGX-grid, which could open a modal-window with a textarea, where you can paste the csv-code and import it as json for MIGX or have an upload-button for uploading a csv-file and importing it at the same time into the MIGX-grid.

        Hi, i know this is an old thread, but hope this trick can be explained smiley Dont know exactly where to begin.
        The client want to add extra data from a csv/excel-sheet without having to enter each row by hand, one at a time.
          Addict since 2012....
          • 3749
          • 24,544 Posts
          Just create a snippet called getCsvFileItems and paste the long code section from Bruno17's post above into it.

          Then copy this tag into the resource content of the page where you want the data to appear:

          [[!getCsvFileItems?
              &file=`assets/files/csvtest.csv`
              &columnheadings=`1`
          ]]


          Change assets/files/csvtest.csv to the actual path of the file (from the MODX root). If the data doesn't change often, you can remove the exclamation point from the tag and just clear the site cache when it changes. That will cut the page load time considerably.

          This assumes that the first line of the CSV vile contains the headings for the columns (it should). If not, I'd suggest changing the options when you export to CSV so that the headings are included.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 38705
            • 101 Posts
            Hi BobRay, thanx for the pointer but i am actually interested in an csv-import button above my MIGX-grid as quoted by Bruno17 in my previous post. The csv-data should be imported into MIGX.
              Addict since 2012....
            • Hi!

              Did you ever get an answer to this? How were you able to create a button to import csv into MIGXdb?

              Cheers,
              Ben
                Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
                • 38705
                • 101 Posts
                Hi Ben,

                No, not yet...
                Solved in another way i believe...

                Still interested though in how to create such an import button for CSV 2 MIGX.
                  Addict since 2012....
                  • 36551
                  • 416 Posts
                  Quote from: Bruno17 at Sep 04, 2013, 12:51 PM
                  something like that snippet should do it:

                  [[!getCsvFileItems? 
                  &file=`assets/files/csvtest.csv`
                  &columnheadings=`1`
                  ]]


                  <!--?php
                  /*
                  [[!getCsvFileItems? 
                  &file=`assets/files/csvtest.csv`
                  &columnheadings=`1`
                  ]]
                  */
                  
                  $file = $modx--->getOption('file', $scriptProperties, '');
                  $delimiter = $modx->getOption('delimiter', $scriptProperties, ',');
                  $columnheadings = $modx->getOption('columnheadings', $scriptProperties, false);
                  $outerTpl = $modx->getOption('outerTpl', $scriptProperties, '@CODE:[[+heading]][[+rows]]<table><tbody></tbody></table>');
                  $headingTpl = $modx->getOption('headingTpl', $scriptProperties, '@CODE:[[+_fields]]');
                  $headingFieldTpl = $modx->getOption('headingFieldTpl', $scriptProperties, '@CODE:[[+_field]]');
                  $rowTpl = $modx->getOption('rowTpl', $scriptProperties, '@CODE:[[+_fields]]');
                  $rowFieldTpl = $modx->getOption('rowFieldTpl', $scriptProperties, '@CODE:[[+_field]]');
                  
                  if (!function_exists('parse_csv_file')) {
                      function parse_csv_file($file, $columnheadings = false, $delimiter = ',', $enclosure = "\"") {
                          $row = 1;
                          $rows = array();
                          $handle = fopen($file, 'r');
                  
                          while (($data = fgetcsv($handle, 1000, $delimiter, $enclosure)) !== false) {
                  
                              if (!($columnheadings == false) && ($row == 1)) {
                                  $fieldnames = $data;
                              } elseif (!($columnheadings == false)) {
                                  foreach ($data as $key => $value) {
                                      unset($data[$key]);
                                      $data[$fieldnames[$key]] = $value;
                                  }
                                  $rows[] = $data;
                              } else {
                                  $rows[] = $data;
                              }
                              $row++;
                          }
                  
                          fclose($handle);
                  
                          $result['fieldnames'] = $fieldnames;
                          $result['rows'] = $rows;
                  
                  
                          return $result;
                      }
                  }
                  
                  if (!function_exists('parseCsvGetChunk')) {
                      function parseCsvGetChunk($tpl, $properties) {
                          global $modx;
                          if (substr($tpl, 0, 6) == "@CODE:") {
                              $template = substr($tpl, 6);
                              $chunk = $modx->newObject('modChunk');
                              $chunk->setCacheable(false);
                              $chunk->setContent($template);
                              $output = $chunk->process($properties);
                          } else {
                              $output = $modx->getChunk($tpl, $properties);
                          }
                          return $output;
                      }
                  }
                  
                  $file = $modx->getOption('base_path') . $file;
                  $result = parse_csv_file($file, $columnheadings, $delimiter);
                  
                  $output = array();
                  $output['heading'] = '';
                  
                  if ($columnheadings) {
                      $heading['_fields'] = '';
                      foreach ($result['fieldnames'] as $fieldname) {
                          $properties = array();
                          $properties['_field'] = $fieldname;
                          $heading['_fields'] .= parseCsvGetChunk($headingFieldTpl, $properties);
                      }
                      $output['heading'] = parseCsvGetChunk($headingTpl, $heading);
                  }
                  
                  $rows = array();
                  foreach ($result['rows'] as $row) {
                      $row['_fields'] = '';
                      foreach ($row as $field) {
                          $properties = array();
                          $properties['_field'] = $field;
                          $row['_fields'] .= parseCsvGetChunk($rowFieldTpl, $properties);
                      }
                      $rows[] = parseCsvGetChunk($rowTpl, $row);
                  }
                  
                  $output['rows'] = implode('', $rows);
                  
                  return parseCsvGetChunk($outerTpl, $output);
                  
                  ?>
                  


                  This snipppet is very close to a solution for a project I'm working on. I've modified the tpl in this to output the content in a series of divs vs a table format. I would like to add a class or classes to each row based on the content of selected cells in that row. Ultimately I will filter or sort these rows/divs based on these classes.

                  Is there someone out there that knows how to do this?

                  Thank you!
                    • 4172
                    • 5,888 Posts
                    if you have

                    &tpl=`yourChunk`


                    you can use output-filters or snippets to prepare the wanted classes in your Chunk
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 36551
                      • 416 Posts
                      Right. I know how I might put in a static class on a row, but I would like to put variable class based on the contents of a specific column in each row.

                      I don't know how to write a snippet that would do that__not in my skill set.

                      I use output filters pretty regularly but I'm not sure an output filter could do this.

                      Any suggestions would be appreciated.