Help with Formit Loading Data

I am able to load the data from formitSaveForm into a resource but i need to render also the form values.

I got some custom fields that are not displaying like the company-name everything else renders fine.
What I am doing wrong
<?php
$sql = “SELECT * FROM modx_formit_forms”;
$data = ;

$count = 0;
foreach($modx->query($sql) as $rows) {

$data[$count]['id'] = $rows['id'];
$data[$count]['form'] = $rows['form'];
$data[$count]['company-name'] = $rows['company-name'];
$data[$count]['ip'] = $rows['ip'];
$data[$count]['date'] = date("Y-m-d", $rows['date']);
$data[$count]['time'] = date("h-m-s", $rows['time']);

$jsonToArry = json_decode($rows['values']);

foreach($jsonToArry as $k => $v) {
    $data[$count][$k] = $v;
}
$count++;

}

$out = fopen(‘php://output’, ‘w’);
foreach ($data as $fields) {
fputcsv($out, $fields);
}
fclose($out);

return;

The table “modx_formit_forms” has no column ‘company-name’ and no column ‘time’. So $rows['company-name'] and $rows['time'] won’t return anything.

All your form values should be saved as json in the column “values” (and correctly output by your code).
Have you checked the database directly (for example with phpMyAdmin)? Do the values you need exist in the database?

they are being input by a formit with name values. they show on the formit values check image

I can see from your screenshot, that Encrypted is set to ‘Yes’. This means, that the column ‘values’ in the database is encrypted.

You have two options: If you don’t encrypt your form &formEncrypt=`0` , then your code should work.
If your want your data to be encrypted, then you have to decrypt it in your code when you read it. You can for example check the code in the snippet “FormItLoadSavedForm” to see how you can do this.
Basically you have to call the function decrypt of the class FormItForm.

<?php
$data = array();
$forms = $modx->getCollection('FormItForm'); //get all the stored forms
foreach($forms as $form){
    $form_array = $form->toArray();
    $encrypted_values = $form->get('values');
    $values = $form->decrypt($encrypted_values); //decrypt the column 'values'
    if (is_string($values)) $values = $modx->fromJSON($values);
    foreach ($values as $key => $value) {
        $form_array[$key] = $value;
    }
    $data[] = $form_array;
}
return $modx->toJson($data);
1 Like

I changed the parameter to encrypt to 0 and worked. Now last thing, how can I tell it to be on a table? this is my call snippet which is displaying the values but can i tell it to display only some certain columns?`<?php
$sql = “SELECT * FROM modx_formit_forms”;
$data = ;

$count = 0;
foreach($modx->query($sql) as $rows) {

$data[$count]['id'] = $rows['id'];

// $data[$count][‘form’] = $rows[‘form’];

$data[$count]['ip'] = $rows['ip'];
$data[$count]['date'] = date("Y-m-d", $rows['date']);


$jsonToArry = json_decode($rows['values']);

foreach($jsonToArry as $k => $v) {
    $data[$count][$k] = $v;
}
$count++;

}

$out = fopen(‘php://output’, ‘w’);
foreach ($data as $fields) {
fputcsv($out, $fields);
}
fclose($out);

return;

Instead of adding everything to the array in a foreach loop, you can just select the values you want:

//instead of this loop ...
$jsonToArry = json_decode($rows['values']);
foreach($jsonToArry as $k => $v) {
	$data[$count][$k] = $v;
}
//... only select the values you need
$jsonToArry = json_decode($rows['values'],true); //add 'true' as the second parameter!
$data[$count]['company-name'] = $jsonToArry['company-name'];
$data[$count]['employeename'] = $jsonToArry['employeename'];
//instead of outputting your data like this ...
$out = fopen('php://output', 'w');
foreach ($data as $fields) {
    fputcsv($out, $fields);
}
fclose($out);
return;
//... you can use code like this:
$output = '';
foreach ($data as $fields) {
    $output .= $modx->getChunk('tplTableRow',$fields);
}
$output = $modx->getChunk('tplTableWrapper',array("rows" => $output));
return $output;

For this to work you have to create 2 new chunks:

Chunk “tplTableRow”

<tr>
    <td>[[+company-name]]</td>
    <td>[[+employeename]]</td>
</tr>

Chunk “tplTableWrapper”

<table>
  <tr>
    <th>Company Name</th>
    <th>Employee Name</th>
  </tr>
  [[+rows]]
</table> 
2 Likes

Everything worked like a charm! Thank you so much the formit plugin is really powerfull

1 Like