Dynamic form with data from mysql/json

Just want to see if I am really moving too far away from where Grav shines and make sure if I am not that I am about to go about this the right way.

I am building a site to basically be the front end to mapping some data between a few tables in MySQL. To do this the simplest way I can think of is to pull the data out of the “static” table into JSON which will allow me to create some cascading drop downs (3) for each row in the table that needs to be updated, mapping the values within to the ones from the static table.
With just PHP scripts I would just loop through the one table from MySQL and build it that way. But with Grav I am just not 100% sure.

From what I have found in the docs, I cant really use the forms plugin, and the forms-database plugin would be fine if I didnt want to get complicated with it and just submitting into a single table (altho I have made it capable of inserting and updating but still limited to 1 entry).

Would I be looking at creating a plugin that would build the table and output the HTML for this onto the Pages? Or is this a combination of plugin + Twig?

There will be multiple similar pages for each “product” that needs to be mapped. The more I think about it, the more I can see that a plugin is the only way this can be possible.

@Chris_W, I’m afraid the goal you are trying to achieve is not very clear to me and I can therefor only give some general pointers.

  • Form fields can be initialised by calling a static php function: See Using function calls
  • A submitted form can be handled in a custom plugin by adding an event handler for event ‘onFormProcessed’. See Custom Action.
  • Forms are not required to use. You can create your own plugin, fetch data, pass the data to Twig and create you own form. The submitted form can then be caught in the plugin.

Thanks @pamtbaau tbh, I think you have answered my question. To make it a bit clearer hopefully, I have a table in MySQL that I need to update. It has 5 columns (one is uid). 3 of the values need to be updatable from a web page (hence looking at Grav). On the site it will basically display a table with the 3 selectable fields being cascading dropdown’s:

+------+-----------+-----------+-----------+
| name | Dropdown1 | Dropdown2 | Dropdown3 |
+------+-----------+-----------+-----------+
| Dog  | Value1    | Value     | Value     |
+------+-----------+-----------+-----------+
| Cat  | Value2    | Value     | Value     |
+------+-----------+-----------+-----------+
| Fox  | Value2    | Value     | Value     |
+------+-----------+-----------+-----------+

The selectable values for these cascading dropdowns can come from the MySQL DB, but its just as easy for me to pre-generate the JSON for this to reduce the time spent waiting on the database.

So what I am looking for guidance on how to do (and the answer looks to be plugin) is to pull the table values out of the database, create the table and the form with all of these dropdowns. This needs to be repeatable as there will be multiple instances of this required, one for each product that requires fields mapped.

From reading the code on a number of plugins, it looks like I may be able to do this by entering some database fields into the Frontmatter which are used to determine which table and fields to pull from the database.

So the answer to my question looks to be “create a plugin”, now its just a case of finding some plugins that do something similar that I can use as a guide to building this out. What I was mostly worried about is that what I am looking to do is far out from what Grav is able to deliver and that I would go and spend days creating a plugin only to find I would have been better off with a different CMS.

Thanks @pamtbaau it appears discourse ate my reply, I will try articulate this again in the hopes of making it clearer, but I do think you have answered my question.

The site I need to make will have a number of pages which will in effect be forms. These are going to be a table that is built from data in a MySQL database. The table that is displayed will have 3 fields that are cascading dropdowns, the values of which will come from a pre-generated JSON file.

So in effect, each page will display a table with 3 drop downs that, when selected and saved, will store the selection back into the MySQL database.

+------+-----------+-----------+-----------+
| name | Dropdown1 | Dropdown2 | Dropdown3 |
+------+-----------+-----------+-----------+
| Dog  | Value1    | Value     | Value     |
+------+-----------+-----------+-----------+
| Cat  | Value2    | Value     | Value     |
+------+-----------+-----------+-----------+
| Fox  | Value2    | Value     | Value     |
+------+-----------+-----------+-----------+

So I think ultimately, the answer to my question is “create a plugin”. What I was more concerned about was that the answer would be look at a different CMS as Grav isn’t ideal for this. Which would have been fine, as I would rather know that now before spending days trying to make it work when it just cant be / shouldn’t be done.

It is just a case of now finding some example plugins to look through to get an idea of what I need to do in order to get it to display as I need to and build up the page. From looking at the forms and forms-database plugins, it looks like I can create the plugin such that I only need to enter some Frontmatter to select the table and columns to display and it should build out the page which is perfect.

Thanks @anon76427325 it appears discourse ate my reply, I will try articulate this again in the hopes of making it clearer, but I do think you have answered my question.

The site I need to make will have a number of pages which will in effect be forms. These are going to be a table that is built from data in a MySQL database. The table that is displayed will have 3 fields that are cascading dropdowns, the values of which will come from a pre-generated JSON file.

So in effect, each page will display a table with 3 drop downs that, when selected and saved, will store the selection back into the MySQL database.

+------+-----------+-----------+-----------+
| name | Dropdown1 | Dropdown2 | Dropdown3 |
+------+-----------+-----------+-----------+
| Dog  | Value1    | Value     | Value     |
+------+-----------+-----------+-----------+
| Cat  | Value2    | Value     | Value     |
+------+-----------+-----------+-----------+
| Fox  | Value2    | Value     | Value     |
+------+-----------+-----------+-----------+

So I think ultimately, the answer to my question is “create a plugin”. What I was more concerned about was that the answer would be look at a different CMS as Grav isn’t ideal for this. Which would have been fine, as I would rather know that now before spending days trying to make it work when it just cant be / shouldn’t be done.

It is just a case of now finding some example plugins to look through to get an idea of what I need to do in order to get it to display as I need to and build up the page. From looking at the forms and forms-database plugins, it looks like I can create the plugin such that I only need to enter some Frontmatter to select the table and columns to display and it should build out the page which is perfect.

For some reason editing a post makes it completely disapear…

@Chris_W, Based on the limited information, here is some extra info.

I don’t think the Form plugin is the way to go. Calls to static methods to populate dropdowns are executed when Form is loaded. They are not (re-)executed in response to changes in other fields.

My initial thoughts are:

  • Create a Twig template that can be used by multiple pages
    • Generate form in Twig based on:
      • parameters in frontmatter of page,
      • and/or based on data from plugin that passes data to Twig.
    • Fill cascading dropdowns dynamically using async javascript fetch.
      Handle the fetch using plugin.
    • Submit the form to the backend either sync or async.
      Plugin will handle submitted data.
  • Create a plugin which can:
    • Fetch data from MySQL and:
      • pass data to Twig,
      • and/or return json to async calls.
    • Respond to submited data (sync or async) and store data in MySQL.

To get started with plugins, install plugin Devtools and run $ bin/plugin devtools new-plugin. This will setup a plugin framework for you.
Also, read the chapters on Plugins.

1 Like

Thats perfect, thank you for the guidance. I read through the Plugins chapter last night to try wrap my head around it all. Will do some more research, and start playing with a plugin.

Glad to see that Grav will work for my use case, as everything else about the CMS has been a perfect fit.

Once again, thank you very much for your help.