We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1934
    • 23 Posts
    Hello,

    I use a snippet called "getCsvFileItems" and have a problem in filtering, where i leave the price blank.

    So if heading is "Verkocht" and the field is "Ja" how do i blank the "price" ??

    Can somebody help me ?
    In the attachment is example of table for the front-end of the site

    Appeltje (from the Netherlands)

    Bouwnr;Prijs;Voorwaarde;Opp;Inhoud;Woonopp.;Type;Tuin;Straat;Verkocht;Kamers;Opslag
    7;376.900;VON;248;602;141;2/1 kap;Zuid;Kneppelweide;Ja;4;Garage
    8;376.900;VON;248;602;141;2/1 kap;Zuid;Kneppelweide;Gereserveerd;4;Garage
    31;376.900;VON;242;602;141;2/1 kap;Zuid;Steertweide;Ja;4;Garage
    32;374.800;VON;242;602;141;2/1 kap;Zuid;Steertweide;Nee;4;Garage
    49;369.100;VON;259;602;141;2/1 kap;Zuid;Zaaijweide;Nee;4;Garage
    50;369.100;VON;249;602;141;2/1 kap;Zuid;Zaaijweide;Nee;4;Garage
    


    <?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 class="table table-bordered table-condensed table-responsive">[[+heading]]<tbody>[[+rows]]</tbody></table>');
    $headingTpl = $modx->getOption('headingTpl', $scriptProperties, '@CODE:<thead><tr class="bouwnummer">[[+_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);