Buy Now
Sign in

"radios group" with data from DB

hekla , September 15 2017
H
hekla
Hi

I have a form where I want load some of the data from a DB with DBRead and event_id:{data:event_id}.

Now I want to create a "radios group" with data from the DB.
There are one to five entries in the DB, so we will get one to five radio buttons, depending on the "event_id"

Question 1)
How do I need to store the data in the DB to use it to populate the radios group, so that there is the same number of radios as in the DB:
- can it be in one table (as an array?) and how would it be formatted then?
- should it be in different tables for each entry

Question 2)
What functions do I need to set up in the "Setup" section of the form.
And what are the entries in the different fields (Where conditions, Select type, Fields to retrieve, Group fields, Special fields etc.)

Question 3)
What fields do I need to set up in the "Designer" section of the form (Options, Selected value, under "Advanced" tab: Reload event, Extra attributes, Data values etc)

I read the CFv6 manual (v1.0.2) but there are so many of my questions open, that I give up trying.

I would really appreciate help in solving this request.

Thank you
Hekla
admin
Hi Hekla,

It does not matter how the data is stored in the database, if you supply an array to the "Options" setting of the radios group then it should display a list of radio buttons.

The way to do this is just as the way to build a dynamic data dropdown, instead that you use a radio element here, did you check that in the manual ?

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
H
hekla
Hi Max

thanks four your answer.
I posted the questions above, because I have been trying for many hours, have read the manual again and again to figure out how to do it – but somehow do not get a to a working solution.

Can you tell me what exact I should do when you say to
supply an array to the "Options" settings

This exactly is the crucial element for me, where I keep failing:
How exactly do I get the data from the DB into the "Options" that it will look like an array and is displayed as a list of radio buttons
admin
Hi hekla,

If you drag a PHP action before the "Display section" action, then name the PHP action to "options", and add this code:

return ["x" => "x", "y" => "y"];

Then use {var:options} in the "Options" box of a dropdown or radios in your form then the options available will be x, y

So what you need to do is to supply an array in the same format using any other mean, the "Read data" select type "key/value pairs" options will do that.

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
H
hekla
Thanks a lot Max.

I am one step further.

I tried according to our description with
return ["230€ – full price  (reg1)" => "230€ – full price", "170 € - reduced price  (reg2)" => "170 € - reduced price", "120 € - sponsored price  (reg3)" => "120 € - sponsored price"];
and it worked.

Then I tried to get the same data from the DB with "Read data".

If I chose "Select type" as "First matching record" I get just one radio button with the label "Array".

If I chose "Select type" as "Array of key/value pairs" I get the exact content of the DB field (["230€ – full price (reg1)" => "230€ – full price", "170 € - reduced price (reg2)" => "170 € - reduced price", "120 € - sponsored price (reg3)" => "120 € - sponsored price"];) – as far as I understand is the DB field content treated as one key and one identical value, so not applicable in our case

The debugger tells me for the first and working example (the PHP action):
[option] => Array
(
[returned] => Array
(
[230€ – full price (reg1)] => 230€ – full price
[170 € - reduced price (reg2)] => 170 € - reduced price
[120 € - sponsored price (reg3)] => 120 € - sponsored price
)

[var] => Array
(
[230€ – full price (reg1)] => 230€ – full price
[170 € - reduced price (reg2)] => 170 € - reduced price
[120 € - sponsored price (reg3)] => 120 € - sponsored price
)

)


In the second not working example with "Read data" and "First matching record" ("loadprices" is the model name):
[read_data11] => Array
(
[log] => Array
(
[0] => SELECT `loadprices`.`prices_adult` AS `loadprices.prices_adult` FROM `z0reg_chronoengine_events` AS `loadprices` WHERE `loadprices`.`event_id` = '17100';
)

[var] => Array
(
[loadprices] => Array
(
[prices_adult] => ["230€ – full price (reg1)" => "230€ – full price", "170 € - reduced price (reg2)" => "170 € - reduced price", "120 € - sponsored price (reg3)" => "120 € - sponsored price"];
)

)

)

Somehow i get the content in a subarray …

My question: how can I retrieve the data from the DB, that I get the same result as with the PHP action.

Thank you
H
hekla
Or the question in short:

What statement in the "PHP action" will get the content of the desired filed.

in pseudo code (I'm not a coder) something like:

from the given DB, return the content of the field `prices_adult` from the row `z0reg_chronoengine_events` where `id` = {data:event_id}


or what I actually need at the end:

from the given DB
if date-of-today < the date in the DB field `earlybird-date` {
return the content of the field `prices_adult_earlybird` from the row `z0reg_chronoengine_events` where `id` = {data:event_id}
}
else {
return the content of the field `prices_adult` from the row `z0reg_chronoengine_events` where `id` = {data:event_id}
}
admin
Hi hekla,

In the "Read data" conditions:

id:{data:event_id}

And set the "select type" to "first matching record".

Now you have all the fields of that record, you can run your condition:
if date-of-today < the date in the DB field

Using PHP code on the result returned in a PHP action:

if(time() < strtotime($this->get("read_data_name.Model.earlybird-date"))){
return $this->get("read_data_name.Model.prices_adult_earlybird");
}else{
return $this->get("read_data_name.Model.prices_adult");
}

Then use {var:php_fn_name}

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
H
hekla
Thanks a lot Max,

I'm close to what I want, and I start getting excited.

Unfortunately it isn't the solution yet – I do not get an array with this code.

The given code
return $this->get("read_data11.loadprices.prices_adult");


retuns the data from the DB field prices_adult
["x2" => "x2", "y2" => "y2", "z2" => "z2"]


I get only one radio button with the label > "x2", "y2" => "y2", "z2" => "z2"]; and the value ["x2" =

when I check the output with {var.pr:prices_adult} I get:

["x2" => "x2", "y2" => "y2", "z2" => "z2"];


When I write the content of the DB field directly into the PHP action:
return ["x2" => "x2", "y2" => "y2", "z2" => "z2"];

I get the correcty 3 radio buttons.
with {var.pr:php25} I get:
Array
(
[x2] => x2
[y2] => y2
[z2] => z2
)


Question:
So how do I modify
return $this->get("read_data11.loadprices.prices_adult");

that I get the same result like when using
return ["x2" => "x2", "y2" => "y2", "z2" => "z2"];
H
hekla
Hi Max,

did you understand my question? I desperately need a solution. It is the last obstacle in my form!

With a DB table prices_adult with the content ["x2" => "x2", "y2" => "y2", "z2" => "z2"]; and
the PHP action return $this->get("read_data11.loadprices.prices_adult");

I get an different resultate than with the PHP action:
return ["x2" => "x2", "y2" => "y2", "z2" => "z2"];

But I would like to get the same result with the DBRead than with return ["x2" => "x2", "y2" => "y2", "z2" => "z2"];

How do I do it?
H
hekla
Please! I'm desperate, if I don't get it to work!
H
hekla
Ok, some more data.

1)
with the PHP action
return ["x2" => "x2", "y2" => "y2", "z2" => "z2"];
I get
Array
(
[x2] => x2
[y2] => y2
[z2] => z2
)

That's the result I want, BUT with data from the DB

2)
with the PHP action
return $this->get("read_data11.loadprices.prices_adult"); 
and the content of prices_adult
["x2" => "x2", "y2" => "y2", "z2" => "z2"]
I get
Array
(
[0] => ["x1" => "x1", "y1" => "y1", "z1" => "z1"]
)
That is NOT what I want.

So: how do I have to modify the PHP action return $this->get("read_data11.loadprices.prices_adult") that I get the same result like in example 1)

I really hope to get this solved
admin
Hi hekla,

Apologies for the late reply!

The easiest solution is to use $this->get("read_data11.loadprices.prices_adult.0") to get the value you need.

But there may be a better solution when returning the value, however this needs more info on how the data is stored and the data read action settings!

Best regards
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
H
hekla
Thank you for your answer.
Unfortunately it doesn't work – the array stays empty.
I just don't know what to do to get it to work.

how the data is stored

["x2" => "x2", "y2" => "y2", "z2" => "z2"];
And I can change it to whatever is needed

the data read action settings

Where conditions: event_id:{data:event_id}
Select type: First maching record
Fields to retrieve: prices_adult and prices_adult_earlybird
admin
Hi hekla,

Unfortunately it doesn't work – the array stays empty.

How do you know ? according the data format you should be able to get the array using the new syntax.

After the "read data", add a "custom code" and use this code:
{var.pr:read_data11.loadprices.prices_adult}

What do you get ?

Are you using this to reload a field ? if yes then just move the new custom code above the custom code used to render the new field.

Best regards
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
H
hekla
Oh yes, it wasn't correct:
I get the first character of the DB field content which is "["

From {var.pr:read_data11.loadprices.prices_adult} after "read data" I get the correct content of the DB field:
["x1" => "x1", "y1" => "y1", "z1" => "z1"];

I tested "custom code" after read data with 0 added at the end: {var.pr:read_data11.loadprices.prices_adult.0}
I get "[" which is the first character of ["x1" => "x1", "y1" => "y1", "z1" => "z1"];

I tried with 1 added at the end: {var.pr:read_data11.loadprices.prices_adult.1}: I get a quotation mark = the second character, with 2 at the end I get "x" = the third character …

And no, it is not to reload a field, just to get the data from the DB for a "Radios group".

Thank you
admin
Hi hekla,

Apologies for the late reply!

What is your "read data" "select" settings at the moment ?

Best regards
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
H
hekla
Hi Max

it is "first matching record"
admin
Hi hekla,

If its set to the "First record" then you do not need the 0.

I think the problem is with the format in which the data is stored in the table, how did you save this data in the table:
["x1" => "x1", "y1" => "y1", "z1" => "z1"]

?

Best regards
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!