We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33203
    • 13 Posts
    Hi guys, i have alreday posted this into another thread with no luck, and at the moment i am almost giving up to this, anyway I hope that somebody reading this post can help me understand what’s wrong with my code.

    PROBLEM:

    I want to populate a dynamic "grid" with the values i have in a simple .csv file

    SOLUTION:
    I created a new TV (ProductsTV) with a datagrid widget COLUMN NAMES (PRODUCT,DESCRIPTION,PRICE) FIELD NAMES (PRODUCT,DESCRIPTION,PRICE) default value is as follows:

    @FILE /assets/files/Products/Products.csv

    The result is that i have a datagrid like this:

    PRODUCT | DESCRIPTION | PRICE
    P
    r
    o
    d
    u
    c
    t
    1
    ,
    d
    e
    s
    c
    1
    ,
    p
    r
    i
    c
    e
    1


    being my CSV file as follows:

    PRODUCT,DESCRIPTON,NAME
    product1,desc1,price1
    product2,desc2,price2

    So, since the datagrid it’s not behaving as i wanted to i thaught myself i had to call some code in there and after reading all the posts in this thread: http://modxcms.com/forums/index.php/topic,1666.0.html solution number two was to do like this (please note this is the code i have into my default value datagrid widget):

    @EVAL 
    $handle = fopen("/assets/files/Prodotti/Products.csv", "r");
    $row = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
       if($row) {
       	$results[] = $data;
       }
       $row++;
    }
    fclose($handle);
    return $results;


    the result is that now i have a table like in the image i am attaching, the table got the correct COLUMN NAMES, but "No recors found" is displayed.

    So, i started thinking myself that maybe something was wrong with:

    1) lines being longer than 1000 chars

    2) the way \t \r \n are handled between modx, my web server and php settings

    So, i decided to do some debugging, so i created a new snippet and called it [[dumpmycsv]] with this code in it:



    <?php
    $handle = fopen("/assets/files/Prodotti/Products.csv", "r");
    $row = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
       if($row) {
       	$results[] = $data;
       }
       $row++;
    }
    fclose($handle);
    return var_dump($results);
    ?>
    


    this is the result:

    array(2) {

    * => array(3) {
    * => string(9) "product1" [1]=> string(5) "desc1" [2]=> string(7) "price1" } [1]=> array(3) {
    * => string(9) "product2" [1]=> string(5) "desc2" [2]=> string(7) "price2" } }


    since the array is populated correctly i started thinking that there must be something wrong with the use of @EVAL and not with the code i am using.

    So, after a terrible headache and 4 days spent trying to figure out what am i missing i decided to post this thread to see if anybody here that has ever used the datagrid widget with @FILE and @EVAL can tell me what am i doing wrong.

    Thanks in advance.