Forums

DB Save depending on dropdown

fredfitaire 14 Nov, 2013
Hello,

Is there a way to do a DB Save to different DB tables depending on dropdown choice ?

For example, if choice 1 is made in my dropdown "input_select_1", the datas are saved to table j25_2013nov. If choice 2 is made in the same dropdown menu, the datas are saved in j25_2013dec, etc ...
(all the tables would have been created previously, based on the same model).

Thanks😉
GreyHead 20 Nov, 2013
Hi fredfitaire,

I don't know of any way to do that using the DB Save action - but you can do it easily enough by hand-coding the DB Save in a Custom Code action.

Bob
fredfitaire 20 Nov, 2013
"easily" ? ... mmmm ... not so sure !!
I would need at least the code of the DB Save function.
Could you provide it ? (and please also give one or two clues to help in the customisation).
Thanks😉

Edit: I guess the code is under administrator/components/com_chronoforms/form_actions/db_save.php, right ? I'm afraid I'm unable to modify it 😢
GreyHead 21 Nov, 2013
Hi fredfitaire,

It starts around line 68 of /administrator/components/com_chronoforms/form_actions/db_save/db_save.php

But, before you go there, why do it that way. It would be much easier (and better design) to use one table with a Year column.

Bob
fredfitaire 22 Nov, 2013
Bob,
Ok, I can add a "Year" column to the table but I don't see how it could solve my problem ... ! Please give more details.
pat01 22 Nov, 2013
Hi Fred

Ok, I can add a "Year" column to the table but I don't see how it could solve my problem ... ! Please give more details.


Use two dropdown menus: one for the years and one for the months.
In this case you use one DB table, but you will be able to sort and filter by year and by month.


You could save into different tables by using a few of the Actions Custom Server Side Validation and nesting them.
Add this code to the first Custom Server Side Validation:

<?php
$year = $_POST[year];
if ($year == 2013) {
return true;
}
else {
return false;
}
?>


(assuming your dropdown is named year and the option values are 2013, 2014 and so on - you will need to change them to match your values)

Add the Action DB Save into the On Success. There you select your 2013 database table.

Drag and drop another Custom Serve Side Validation into the On Fail.

Then repeat above steps.
But this time, add the following code to your (2nd) Custom Server Side Validation:

<?php
$year = $_POST[year];
if ($year == 2014) {
return true;
}
else {
return false;
}
?>


And select your 2014 database table in the Action DB Save here.

And so on…

Please note:
I don’t know how good above solution is regarding the performance.
And you must make sure, the user must select a proper value from the dropdown. One its value matches the PHP Code (example $year == 2014).

And of course above solution is not really good if you have many entries in your dropdown…

Maybe better way would be as Bob suggested by using the Action Custom Code and writing the process with proper PHP there. I guess it depends on the number of entries in your dropdown and I guess you will have many...

Patrick
fredfitaire 27 Nov, 2013
Thank you very much Patrick for your help !
I'll try your solution and let you know.
GreyHead 27 Nov, 2013
Hi Fred,

If you have to use multiple tables, then I'd use one or more Event Switched [GH] actions rather that nesting Custom Serverside validation actions. It's just gets complicated to add to many nested layers.

I would still use a single table rather than several different ones unless there is a very good reason for keeping them separate.

Bob
fredfitaire 28 Nov, 2013
Hi Bob,

I now realize that without a clear request that explains the exact need, there might be plenty of solutions ! The best is that I explain the context:

I manage the website of a club which management team have a monthly meeting. The need is that all the team members can provide the items of the agenda for the next meetings. My form has a dropdown menu in which they can choose the date of the meeting when the item will be discussed, not only the next one but all the dates of the current year. For a better follow up of each item, I would like to keep the agendas of each meetings and publish as much articles as agendas. I use a plugin called "Tools JX" to show them as spreadsheets within an article, through a simple code like {insertgrid ID=1}. The grid is defined in Tools JX to show the entries of the table linked to the chosen month.

My first idea was to create as much tables as dates available in the dropdown. But maybe I'm wrong !

I would appreciate your comments and suggestions.
Thanks !
GreyHead 28 Nov, 2013
Hi fredfitaire,

I don't know anything about Table JX but assuming that you can pass it a WHERE clause in some way I'd still add all these items in a single table and use some kind of identifier to link them to a particular meeting. The worst case is that you'd have two joined tables - one for the meeting info (date, location, etc) and the second for all of the menu items. That would be much better DB design than setting up a new table for each meeting.

Bob
This topic is locked and no more replies can be posted.