Json to placeholders

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.

thanks in advance

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));


here is a screenshot of the column in phpAdmin

thanks

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.

Where should I start with xpdo

I do feel like it’s something I need to get my head around - I’m a design principly but I seem to doing more and more of this with modx

You could read the dedicated section in the MODx Docs. It also helps to have some basic knowledge of SQL.


Maybe I can provide you with some code if you can tell me what table you are trying to query. modx_formit_forms from the FormIt extra?

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.

this is my rowboat call:

[[!Rowboat?
   &table=`modx_formit_forms`
   &columns=`{"values":"text"}`
   &where=`{"form:LIKE":"test retriever"}`
   &tpl=`row_result`
   &debug=`1`
]]

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.

To be honest I feel like formIt has this function formitloadsavedform

But the doc only discusses passing this on a submit not looping through all forms.

thanks in advance

sounds, like it would be better to store your form submits into a custom table

this looks like something i need to read!

Here is some code to loop the forms:

<?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.

There are different ways to create a custom database table.

In the tutorial from Bob (you linked to) you first create a database table in MySQL and then create the schema/PHP-classes based on this table.

Another way is to first write a schema, and then automatically create to database table based on this schema.

There are also extras that can facilitate the process (so you don’t have to deal with code). For example ExtraBuilder or MIGX.

You may want to watch this youtube video about MIGXdb and xPDO.

This great - thanks.

How do add other columns like the ‘id’ and ‘context’ from record and pass them to placeholder
thanks

You could for example do something like 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['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.

$phs['my_placeholder_name'] = "some value"; //[[+my_placeholder_name]]

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

Thanks again

The simplest way to do this requires 2 pages:

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.

1 Like

Thanks

The GET parameter would have to be a custom snippet? its not a parameter of migxloop?

you would link to the edit page with something like

<a href="editpage.html?doodle_id=[[+id]]">Edit</a>
1 Like

Sorry what am I doing wrong

loop tpl

<p>[[+username]] || [[+description]] || <a href="[[~123]]?doodle_id=[[+id]]">Edit Fields</a></p>

formit call

[[!FormIt?
&hooks=FormIt2db
&preHooks=db2FormIt
&packagename=doodles
&classname=doodle
&paramname=doodle_id
]]

do i need where or fieldname?

I go to the form but doesn’t load data

thanks

I believe when using paramname, the name of the GET-parameter has to be the same as the name of your primary-key column.

<a href="[[~123]]?id=[[+id]]">
[[!FormIt?
   ...
   &paramname=`id`
]]
<form>
   ...
   <input type="text" name="id" value="[[!+fi.id]]"/>
</form>
1 Like