Form Data Array in PHP functtion

RobP 30 Sep, 2014
I try to use the Form Data array in a PHP function.
The idea is to make a large form with Bootstrap radio buttons in modal windows
Form Data Array in PHP functtion image 1.
The code for the radio buttons in one window are in a custom code field.
I try to put some of the code in function, but the Form Data array is not accessible in that function.
Is there a way to solve this, other than put these 2 lines ( $val and $model) in every buttongroup.

Rob

<?php
$datamodel="klachten";                   // Model of the survey table
$name="lengte";                          // Name of the field in survey table
$model="lengte";                         // Model of the table used for the radio buttons
$label="Wat is uw lengte?";             // Label for the radio group
radio($datamodel, $name, $model, $label); 


Function radio($datamodel,$name, $model, $label)
{
$name= $datamodel."[".$name."]";        // Field in the Form Data Array (used in external function)
$val= $form->data[$datamodel][$model];  // Value of the field stored in survey table
$model= $form->data[$model] ;            // array with the fields for the radio buttons
echo \GCore\Helpers\bootradio::modelselect($name, $label, $model, $val); 
                                     // Function the create the radio buttons
}	
?>
GreyHead 01 Oct, 2014
Hi Rob,

It should work if you pass the $form->data array to the function
<?php
radio('klatchen', 'lengte', 'lengte', 'Wat is uw lengte?', $form->data ); 

function radio($datamodel,$name, $model, $label, $data)
{
  echo \GCore\Helpers\bootradio::modelselect( $datamodel."[{$name}]", $label, $data[$model], $data[$datamodel][$model]); 
   // Function the create the radio buttons
} 
?>

Bob
RobP 01 Oct, 2014
Hi Bob,

I thought about that to.
But my form is interactive, with a lot of fields that are displayed depending on the value of other fields.
Passing the complete $form-> array for every item is not very efficient.

For the moment I keep the extra lines.

Rob
GreyHead 01 Oct, 2014
Hi Rob,

In the current versions of PHP it's passed by reference anyhow so makes only a tiny difference.

Bob
RobP 04 Oct, 2014
Hi Bob,

Thanks , the references make it easier.
I skipped the radio function and just use the second function call now.
It will be a large interactive form with a lot of show/hide containers and tabs.
Reducing the code makes it easier to copy and paste the fields from my spreadsheet.

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