Hello me again…
I ran rowboat on database table to return this column.
It outputs this string to my tpl (with the brackets)
{“user”:“001”,“name”:“johndoe”,“text”:“test”}
how do I break it into placeholders.
do I use an output filter to interrogate it?
or should I be using a different plugin like pdotools.
Just to confirm, your table column stores that information as JSON?
If so you’ll likely need a snippet to run json_decode() on the value, and then parse it through a chunk with something like $modx->getChunk('name', json_decode($value, true));
Like Mark tells. You will need a snippet where you pass the value of that placeholder as a property and prepare the output for example with $modx->getChunk after json_decode the JSON - string into an array.
Maybe in this case instead of using Rowboat you could also do the database query yourself with xPDO.
This way rather than calling a snippet (Rowboat) that loads a chunk that contains a custom snippet which uses another chunk, you could do the same with only one snippet call.
Hi,
The big picture idea is to allow a logged in user to see all the forms they have submitted, edit/delete then invite comment on there form from another logged in user.
So I am trying to interrogate the forms called “test retriever” and from ‘values’ grab the user id from and then list the forms they posted, but all the values are held as a json.
<?php
//load FormIt package
$base_path = $modx->getOption('core_path') . 'components/formit/';
$modx->addPackage('formit', $base_path . 'model/');
//create a query and restrict the rows by the 'form' column
$q = $modx->newQuery('FormItForm');
$q->where(array(
'form' => 'test retriever'
));
$output = '';
$saved_forms = $modx->getCollection('FormItForm', $q);
foreach($saved_forms as $saved_form){
$values_json = $saved_form->get('values'); //read value of column 'values'
$values = json_decode($values_json, true); //convert json to an array
if ($user == $values['user']){ //test if correct user
$output .= $modx->getChunk('row_result', $values); //parse the chunk
}
}
return $output;
When you call this snippet add a user property [[!YourSnippetName? &user=`001`]].
In the chunk row_result you can use placeholders for all your form fields ([[+text]], [[+user]], [[+name]], etc.).
Like Bruno said, it’s probably a better idea to use a custom table. With this database table you always have to loop through all the rows to select the ones from a certain user.
...
foreach($saved_forms as $saved_form){
$values_json = $saved_form->get('values'); //read value of column 'values'
$values = json_decode($values_json, true); //convert json to an array
if ($user == $values['user']){ //test if correct user
$phs = $saved_form->toArray();
$phs['values'] = $values;
$output .= $modx->getChunk('row_result', $phs); //parse the chunk
}
}
Then you can access all the columns from the database table with [[+id]] or [[+context_key]] and all the json fields with the prefix values ([[+values.text]], [[+values.user]]).
Or you could do this
foreach($saved_forms as $saved_form){
$values_json = $saved_form->get('values'); //read value of column 'values'
$values = json_decode($values_json, true); //convert json to an array
if ($user == $values['user']){ //test if correct user
$phs = $saved_form->toArray();
$phs = array_merge($phs, $values);
$output .= $modx->getChunk('row_result', $phs); //parse the chunk
}
}
and then use all placeholders without a prefix ([[+id]], [[+context_key]], [[+text]], [[+user]]).
Basically, everything in the array $phs becomes a placeholder in the chunk.
Thanks for these pointers they have really helped.
I have managed to create a doodles data base.
I have been able to post more doodles to the database with formit2db - in the front end
I have managed to loop through specific user created doodles with migxloopcollection - in the front end
Now I need a user to be able to click on one of the listed doodles and edit it.( I suppose this is like a socialmediia feed but without any sharing).
I am wondering if I should just loop the chunk in my migxloopcollection with a formIt calls and hook formIt2DB - but I assume this with reload the page
One page to create/edit a doodle with a call to “FormIt”, the hook FormIt2db (to save the record) and the prehook db2FormIt (to load an existing record).
And another page with a list of the doodles. Each doodle has an edit link that calls the edit-page with a GET-parameter to indicate which record to load from the database.
If you want to achieve the same without reloading the page, then you’ll have to call the edit-page with AJAX.