I'm currently building an MVC-ish structure on our project. My objective is to create panels with a function call. However, I'm currently stuck on how to pass a call to a function, that requires two parameters, within a foreach
loop.
Function Call (not working)
$display_panel_create->get_display_create_panel($size=6, $title='Guest',
$field=['join_two_fields' => ['field1' => 'first_name','field2' => 'last_name'], 'email', 'phone',
'company','address', 'comments'], $action='Create', $type='success');
The receiving method is:
function get_display_create_panel($size, $title, $field, $action, $type){
$panel = '';
$panel .= $this->panel_size($size);
$panel .= $this->panel_header($title, $action, $type);
$panel .= $this->panel_form($action, $title);
$panel .= $this->panel_body($field);
$panel .= $this->button($type);
$panel .= $this->panel_footer();
return $panel;
}
As we create new "parts", the body call is a foreach
loop that does the following:
protected function panel_body($field){
$body = '';
foreach ($field as $item){
if (is_array($item)){
foreach ($item as $k => $v){
if ($k == 'join_two_fields'){
$body .= $this->$k($field1 = $v['field1'], $field2=$v['field2']);
unset ($item[$k]);
} else { $body .= $this->$k($v); }
}
} else { $body .= $this->$item(); }
}
return $body;
}
However, a panel is not being created. I tried using test.php file to make sure my looping is correct, and I realized that my issue is that during the initial loop, i'm not sending both variables for $field1
and $field2
.
Can anyone suggest a more elegant solution?
--------- test.php code --------
$array = [$size=6, $title='Guest',
$field=[
'join_two_fields' =>
['field1' => 'first_name','field2' => 'last_name'],
'email', 'phone', 'company','address', 'comments'],
$action='Create', $type='success'
];
foreach ($array as $item){
if (is_array($item)){
foreach ($item as $k => $v){
if ($k == 'join_two_fields'){
echo $v['field1'] . '<br>';
echo $v['field2'] . '<br>';
}
else { echo $v . '<br>'; }
}
} else { echo $item . '<br>'; }
}
--------test.php output---------
6 Guest first_name last_name e e phone company address comments Create success