Forums

Conditonal DB Save

Yudhizth 15 Jan, 2016
Dear all

I confused when I try to save into different table with conditional case. I have a multi-choice dropdown that have an event on each value. and I have 4 textbox with different name.

If dropdown value 1 then show parent textbox1
If dropdown value 2 then show parent textbox2
If dropdown value 3 then show parent textbox3
If dropdown value 4 then show parent textbox4

On submit event, I'm using event switcher with condition :

<?php
if($form->data("dropdown") == 1){
return "first";
}
if($form->data("dropdown") == 2){
return "second";
}
if($form->data("dropdown") == 3){
return "third";
}
else{
return "fourth"
}


So, because I'm using multi-choice, the textbox can show more than one, I have no idea with this condition. I hope you understand my question and can give me a clue to solve this. Thank you
GreyHead 15 Jan, 2016
Hi Yudhizth,

I'm sorry, I don't understand the question - where are these text boxes? If they are in the form HTML then presumably you want to show them in the browser and not after the form submits?

Also if you have a multi-choice drop down then it needs to have an array name like dropdown[] or only the last value will be submitted.

Bob
Yudhizth 15 Jan, 2016
Ok, sorry if my question is ambiguous.

1. The form contains dropdown (multi-choice), and 4 textbox (1-4).
2. The textbox show when user click one or more of the multi-choice value.
3. Let we assume that user choose textbox1 and it's show on the page.
4. So when user submit the form. it will save into table_textbox1 for example. (depends on textbox that shown, if textbox2 it will save into table_textbox2 etc)

The question is, how to detect if texbox1 or 2,3,4 not null then save to each table. And can save to multi table if user choose more than one on multi-choice

Thank you
GreyHead 15 Jan, 2016
Hi Yudhizth,

OK - that helps me, thank you.

First question is do you need four separate tables? You could save all the text into one table with columns for id, record_id, number = 1,2,3,4, and text.

With that method you could use one DB Save with multi set; otherwise you will need four DB Saves one for each textbox.

The way to identify what is to be saved is either to make the column names unique, or to use a model id to identify the data to be saved by each query e.g. $form->data['model_id']['text']

Bob
Yudhizth 16 Jan, 2016
Thank you Bob,

Yes I do,
I little bit confused with your statement. Is I have to put event switcher? and save into four table with condition on it? Because every table has different functionality.
GreyHead 16 Jan, 2016
Hi Yudhizth,

If you need to save to four tables then you need four DB Save actions - as long as the column names are unique this should work correctly.

Bob
Yudhizth 16 Jan, 2016
Thank you Bob,

I can't put four DB Save action without condition. because each textbox appear when user choose multi-choice value. I put action show parent on it, because by default I set parent hidden on all textbox. So, if I put DB save without condition, it will save into four database even though the value is null, because another textbox not appear.
GreyHead 16 Jan, 2016
Answer
Hi Yudhizth,

To do this with the Event Switcher, you'd need four of them too. The simplest solution is probably to use a Custom Code action with a switch statement to do all four saves.

Bob
Yudhizth 16 Jan, 2016
Thankyou Bob,

It's almost done. I try to create condition when array is not empty, but I'm getting stuck with my code.

<?php
if($form->data("tablefo") !=""){
return "fo";
}
if($form->data("tablesatelit") !=""){
return "satelit";
}
if($form->data("tableskkl") !=""){
return "skkl";
}
if($form->data("tablevsat") !=""){
return "vsat";
}


So when array is not empty, it will return to each event and saves into each table (DB Save).
This topic is locked and no more replies can be posted.