Buy Now
Sign in

How to reference an array in order to add values in a column

farscape , September 06 at 01:24
Answered
F
farscape
I have a survey form where the user checks off choices. When s/he clicks on the submit button, the form saves the results, then displays the checked options. At this point, the user can further check the options that s/he considers his/her top three, then submit again to update the "top" records in the table.
The checkboxes save a value of "1". I need to validate to ensure no more than three have been selected, and that's where I'm stuck. I know I first need to test whether or not the total exceeds three, and use the answer to drive a switch_event.
I know that to reference an array, I need to use $this->get("arrayname", "default"). The PHP I am trying to use in an PHP action is:
if (array_sum(array_column($this->get("arrayname", "default")  , 'top3')) <=3)​
​{return "fail";}​
​else​
​{ return "pass";}​
​}
My problem is that I cannot figure out how to identify the array. The documentation and forum posts I can find suggest that "arrayname" should be an action name, something like save_data29, but when I place the debugger as the first action of the relevant submit section. The resulting array has no action-related name:
Array
(
[option] => com_chronoforms6
[cont] => manager
[chronoform] => working-survey
[response_id] => 282
[event] => submit
[responses] => Array
(
[0] => Array
(
[response_id] => 23369
[top3] =>
)

[1] => Array
(
[response_id] => 23370
[top3] => 1
)

[2] => Array
(
[response_id] => 23371
[top3] => 1
)

)

[service_area] => Preschool Teacher Assistant
[submit] => page2
[_ga] => GA1.2.226007831.1505575861
[__atuvc] => 3|30,0|31,0|32,1|33,7|34
[fbfb6ec875e9779d7005eb6b867b3811] => 7f1ab2111bb644771c5980efa8d9104f
[_gid] => GA1.2.1539954469.1536195266
[426df58a549166024cb9cd825c5f8cae] => 19a6186025c963b92c3ff23898271779
)
Using the above array, I would expect the nested functions to calculate a sum of "2", and thus "pass".
I have tried several options to read the [responses] array:
  1. $this->get("responses", "default")
  2. $this->get("[responses]", "default")
  3. $this->get("responses.[n]", "default")
But the actual result is:
Array
(
[php45] => Array
(
[returned] => default
[var] => default
)

)
When I put a debugger after the save action, I see an array with an action name like save_data29 (see below). I did try putting the PHP action below the save, and try save_data29, even though it seems to me it defeats the validation since the data has already been saved, but the php45 array shows the same result.
So how should I identify the "responses" array in order to add the values in the column? Or is there a better approach?
Thanks in advance.
Array
(
[save_data29] => Array
(
[data] => Array
(
[response_id] => 23371
[top3] => 1
[modified] => 2018-09-06 00:55:26
)

[_success] => Data saved successfully
[log] => Array
(
[0] => UPDATE `inyht_cf6_service_detail` AS `Data29` SET `top3` = '1', `modified` = '2018-09-06 00:55:26' WHERE `aid` = '23371';
)

[var] => Array
(
[top3] => 1
[modified] => 2018-09-06 00:55:26
)

)

)
healyhatman
but when I place the debugger asthe first action of the relevant submit section
If a thing hasn't happened yet, the data for it won't be in the debug action for you to look at. So I would put the debug action last.
$this->get works for returned values of your functions - where otherwise you would be using the {var:} shortcode. What you're looking for to get the responses array is $this->data("value_name", "default") which is for getting form data.
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
F
farscape
Thanks Healyhatman. That did it. For anyone reading this later, for my form, the working answer is the following in a PHP action, followed by a switch_event that used 'pass' and 'fail' as the events.
if ( array_sum(array_column($this->data("responses", "default"), 'top3')) <= 3 ) {​
​ return 'pass';​
​} else {​
​ return 'fail';​
​}