Forums

Read data array from table with Connectivity

ErixSr 19 Oct, 2022
Hello everyone and sorry for my ignorance, but I'm wasting an afternoon without finding anything to help me. I managed to link a table on the db in CF7 and following the videos on youtube to create with connectivity the list of fields in the table except the json data part:
In the "Table List" I cannot find the suitable formula to report in the Patch field to connect the json array {"1": "Roberto", "2": "Venturae", "8": "Via Torino", " 9 ":" 3273333333 ...)
How to do?

Grazie
Colnem 20 Oct, 2022
1 Likes
Hi
To create a field to receive a json array in a SQL table, you have just to create a field in Text format.

If you have an array like
1=>Roberto
2=>Venturae
8=>Torino

You have to save this field like {data.join[,]:array_var_name}

You can explode this field like {data.split[,]:table_field_name}.
ErixSr 20 Oct, 2022
Hi, thanks for the reply.
Sorry but I don't understand. The problem is with Connectivity. I can connect and read the "Calessini" table. In the Repo section I have to show every single field of the record and then I recall the path of every single field as "ID" or "created" or "ipaddress", but if in the next path I type the name of the "DATA" field, the listing will show the 'array: "1": "Eriberto", "2": "Genovese", "8": "sia3", "9": "327351122 .... Instead in that column I should get only the single field" 1 "(which in the form is" name "):"Eriberto ".

Colnem 20 Oct, 2022
1 Likes
OK. I didn't understand...

The principle:
1 - You have to use a PHP action to rebuild your array, using json-decode php function (look at this: https://www.php.net/manual/en/function.json-decode.php)
2 - You have to write this array in a data field of your form
3 - You have to use something like {data:data_field.1}

Do you know to do this?
ErixSr 20 Oct, 2022
Then I take advantage of your patience. ;-) I apologize because this is not my main job and my preparation is .... :-(
I read the article you recommended and I understood the logic. If I'm not wrong I should decode the json string:

$ strjson = '{"1": "Eriberto", "2": "Genovese", "8": sia3 "
} ';

$ carrel = json_decode ($ strjson);

echo $ carrel-> 1; (result) // Eriberto
echo $ carrel-> 2; ( " ) //Genoese
echo $ carrel etc.

If this is right and not knowing Connectivity yet,
where do i find the "data field of your form"?

Finally, it is the string: $ carrel-> 1; the one that will be reported in the Path of the Table Columns?

Thanks again
Colnem 21 Oct, 2022
1 Likes
Hi

where do i find the "data field of your form"?


I mean the data json field of your Cassini table.

1 - Decode the json field to have an array (with {data,split[,]:table.model.json-field} or with a PHP action) and put this array in a var.
2 - Use this var in your table column

I've no tried. Just a principle...
ErixSr 21 Oct, 2022
In theory I understand what the procedure is. Basically I have to understand the outline, or how to create the new text field in the "Calessini" table and insert the decoding to create a var ... I know that for a beginner like me it is too complicated ... But it could not be left out this Json from the database ??????
Ps. But, you're Italian?
Colnem 22 Oct, 2022
No, French...

And my english is very bad. I'm not sure that I have fully understand what you want.
ErixSr 22 Oct, 2022
... and I'm Italian and I speak English worse than you. 😛🤣🤣. The good news is that I am learning things that may come in handy in the future😉
ErixSr 22 Oct, 2022
Ok, let's try to understand if we are talking about the same thing.
I have created a measurement form that the customer sends me. It works perfectly and I see it from the "Log" link in the forms menu:

Now I want to create the Report of all the modules received, so I use Connectivity
I create the form following the tutorial on youtube: https://www.youtube.com/watch?v=raXqQcU_OB8&list=PLNdPw6Bog3zcuSKs7NGNi8QPtfj5IsxQA&index=10
and everything works until, in the database, it has to read the "data" field where the Json string is.

As you already wrote, I have to decode the Json string and convert it to "var" and report every "var" in every "Path" of the "Table Colums" Okay?

And now the problem: having the Connectivity form in use (which I know very little) in which section and / or field should I report the code that explodes the Json string?

I hope I was clear and I trust in your patience ...
Colnem 23 Oct, 2022
OK. This is what I understood...
Colnem 23 Oct, 2022
OK. This is that I understood...

Suppose that I have a php var like this:
$strjson= '{"1":"Eriberto","2":"Genovese","8":"sia3"}';

I have to decode this var:
$tmp = json_decode ($strjson,true);

The result is:
[em]Array ( [1[/em]
  • => Eriberto [2] => Genovese [8] => sia3 )

    To use an array in a radio group (or a checkboxes group, …), this array do have a model, like this:
  • Array
    (
    [0] => Array
    (
    [model] => Array
    (
    [value] => 1
    [option] => Eriberto
    )
    )
    [1] => Array
    (
    [model] => Array
    (
    [value] => 2
    [option] => Genovese
    )
    )
    [2] => Array
    (
    [model] => Array
    (
    [value] => 8
    [option] => sia3
    )
    )
    )


    So I have to rebuild my $tmp array like this:

    $myvar=array(); $i=0;
    foreach ($tmp as $value => $option) {
    $myvar[$i]['model']['value']=$value;
    $myvar[$i]['model']['option']=$option;$i++;
    };


    To use $myvar in CF7, I do this:
    $this->set("myvar",$myvar);
    So {var:myvar} is an array with a model named model.

    In my radio group, I delete all manual Options and I add a Dynamic Option:
    Data source: {var:myvar}
    Option value: {var:model.value}
    Text Value: {var:model.option}


    Enjoy!
    ErixSr 23 Oct, 2022
    Excuse me but I made myself ill. I have to make a report that gives me the list of forms compiled by customers. For this I am using Connectivity. The initial form filled in by the customer is stored in the database with the fields we talked about including the "date" field with the JSON string. With the Connectivity form I have to take that data and make a report. Everything works with the first single fields (see photos) in the 1st Path I insert "AID" which is the name of the column and in the report it gives me the id n. 1, in the 2nd Path I write "created" and in the report it reports the date of creation of the form, in the 3rd Path I write "ipaddress" and in the report it reports the IP of the customer. The problem is what should I write in the 4th Path to have, in the report, the name "Eriberto" (which is part of the Json string)?

    I understand the system for decoding the Json string. The simple problem is where do I have to write the formulas in Connectivity to make them work ?.
    ErixSr 24 Oct, 2022
    Optimal.
    Now it remains for me to understand where to insert the decoding code of the json string. (in which area of Connectivity)
    Colnem 25 Oct, 2022
    Hi

    To insert the json_field in PHP action, you have to pass the variable read_data.model.json_field to PHP.
    At the top of my code, replace...
    $strjson= '{"1":"Eriberto","2":"Genovese","8":"sia3"}';
    by
    $strjson=$this->get("read_data.model.json_field","");

    At the end of the PHP action, the result is in the CF7 variable myvar.
    Insert an Action Message after the PHP Action to see it, in wich you write:
    My var =
    {var.pr:myvar}


    OK?

    Then you have to use this var in a TableView field
    View Table List:
    Data Sources: {var:myvar} //You have to write yourself {var:myvar}
    Path: model.option, Header:Option
    Path:model.value, Header:value


    Edit:
    I've add ID field in my Table View: look at...
    https://www.olympe-retraite-sportive.com/index.php?option=com_chronoforms7&chronoform=form-23oct22-0448

    Nota: I've using a Read Database with Select = First Record. I think it's more difficult with Select = All Matching Records. I'm not sure that it's possible because this is a non sense.
    ErixSr 25 Oct, 2022
    Hi Colnem,
    I know I'm getting unbearable ....
    The code is clear to me and I thank you. The problem is where to apply it.

    When you write:
    "Then you have to use this var in a TableView field
    View Table List:
    Data Sources... ecc
    I use the Table List in the Views Section of Connectivity.

    but what I would like to understand is
    where to write the code "$ strjson = $ this-> get (" read_data.model.json_field "," ");"
    In which section and in which field of connectivity should I write it?
    always in the Table list? (I do not think so)

    Thank you
    Colnem 25 Oct, 2022
    You have to add a PHP Action inTop Menu/Repo/Action...

    In your PHP Action, copy this:

    //I Suppose your read Data Base is named read_data_8, with Model named Model8
    $tmp=json_decode($this->get("read_data_8.Model8.json",""));
    echo "<h4> </h4><h4><strong>Json field:</strong> ".'{"1":"Eriberto","2":"Genovese","8":"sia3"}'."</h4>";

    echo "<h4><strong>Json_decode:</strong></h4>";
    print_r($tmp);

    /*Ajout d'un modèle*/
    $myvar=array(); $i=0; $id=$this->get("read_data_8.Model8.id","");
    foreach ($tmp as $value => $option) {
    $myvar[$i]['model']['id']=$id;
    $myvar[$i]['model']['value']=$value;
    $myvar[$i]['model']['option']=$option;$i++;
    };

    echo "<h4> </h4><h4><strong>Variable myvar:</strong> </h4>";
    print_r($myvar);

    /*retour vers CF7*/
    $this->set("myvar",$myvar);
    You need to login to be able to post a reply.