We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25663 MODX Staff
    • 12,272 Posts
    re: http://modxcms.com/datagrid-widget.html (a usage example would be really helpful here)

    What type of file does it take for the widget to work? Does the paging work?

    I call the widget using the following in the default values for the widget TV I created for this. It renders a mostly-empty grid, with only single-characters running down the first column and correct headings.
    @FILE assets/files/board.csv



    FWIW, I’m able to take a simple CSV file and parse it into a table per a snippet below, but the Datagrid fails...

    Data (assets/files/board.csv):
    DATE,EQ, FROM - ST, FROM CITY, TO CITY, TO ST
    10/17/05,FDT,AL,MONTEVALLO,PIGGOTT,AR
    10/20/05,FDT,AL,PINE APPLE ,PIGGOTT,AR
    10/18/05,S/D ONLY,CA,SAN DIEGO,CORTEZ,CO


    Snippet code that works:
    if (file_exists($modx->config['base_path'].$CSVf)) 
      {$csvlines = file($modx->config['base_path'].$CSVf); } 
      else return "Not found $modx->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>';
      Ryan Thrash, MODX Co-Founder
      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • 32963
      • 1,732 Posts
      Field Names: id,pagetitle, longtitle,description

      Default value: @EVAL return $modx->db->query("SELECT id,pagetitle, longtitle,description FROM ".$modx->getFullTableName("site_content"));

      Or

      Default value: @SELECT id,pagetitle,longtitle,description FROM {PREFIX}site_content

      That’s it
        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 25663 MODX Staff
        • 12,272 Posts
        Cool... would make a handy way to do things like, say, a member directory perhaps of web_users.

        Doesn’t look like the paging part works... is that true?
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 32963
          • 1,732 Posts
          It’s working but you must first set the Page size property to a value that’s smaller that the records to be displayed.

          Try setting it to 10 or 5
            xWisdom
            www.xwisdomhtml.com
            The fear of the Lord is the beginning of wisdom:
            MODx Co-Founder - Create and do more with less.
            • 25663 MODX Staff
            • 12,272 Posts
            I did (5)... it didn’t. Then again I suspect it’s because it’s trying to use the CSV file (see original post) which I’ve not managed to turn into an array to be passed to the TV for formatting.
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Why not go ahead and store the csv file in the database? I had a similar need, but the data is mildly confidential, and the security of being in the database is slightly better than in files sitting in a world-readable and writeable text file on the server.

                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 25663 MODX Staff
                • 12,272 Posts
                Cause then I’d have to know how to do that of course! With everything except CSS, I’m so coding challenged it’s not even funny!

                Actually, this is going to functionally be a drop box for the client to export a CSV file to update their site as needed, by just saving their Excel file in a certain way.
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Here is the system I used for a client with a translation termbase they wanted online. I had originally hacked this into the Manager; now it works just fine as a module. In any case, the part you’ll be interested in is this:

                  // open the uploaded file
                  $handle = fopen($uploadfile, "r") or die("unable to open file");
                  // read it and generate the query...
                  while($data = fgetcsv($handle, 10000, ",")) { 
                     $num = count($data); // how many items?
                     $row++;
                     $sql = "INSERT INTO $table VALUES ('','$client',";
                     for($c=0; $c < $num; $c++) {  // for each row...
                      $sql .= "'";
                       $sql .= mysql_real_escape_string($data[$c]); 
                       $sql .= "',";
                     }
                     $sql = substr($sql, 0, -1); // drop the last comma
                     $sql .= ")"; // add the close of the query
                  // insert the data
                  
                     @mysql_query($sql) or die(mysql_error());
                  }
                  fclose($handle); 
                  


                  I know that MySQL has a function to suck up a CSV file, I just haven’t really taken the time to look for it since this works, and I wanted an exercise in file manipulation anyway.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 25663 MODX Staff
                    • 12,272 Posts
                    Very nice Susan... thanks!

                    Out of curiosity, how did you make those files into a module?
                      Ryan Thrash, MODX Co-Founder
                      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                      • 34162
                      • 1 Posts
                      Depending on your mysql config, this could work as a SQL statement:

                      LOAD DATA INFILE ’/importfile.csv’
                      INTO TABLE test_table
                      FIELDS TERMINATED BY ’,’
                      LINES TERMINATED BY ’\n’
                      (field1, field2, field3);

                      (Adapt as needed.)

                      Note that this won’t work on most hosted environments as they tend to not grant the LOAD DATA permission. If you run your own box, however, it’s trivial.