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
The problem is the way you are iterating. Lets break it down:
you first run this:
foreach ($field as $item) {
This will set your $item
as all the array's values such as this mocukup:
"join_two_fields" => $item,
0 => $item,
1 => $item,
2 => $item,
3 => $item,
4 => $item
Then you check if $item
is an array , and if it is you loop through $item
with another foreach' inside
:
foreach ($field as $item) {
if (is_array($item)) {
foreach ($item as $k => $v) {
which then $item
would be something like this:
$k => $v
OR
'field1' => 'first_name'
'field2' => 'last_name'
so far so good, ts working, here comes the problem. After your iterations, you have your condition if ($k == 'join_two_fields') {
which checks if the current key equals "join_two_fields"
, well this will never happen . What you mean to check here is the original key of your first iteration, not the key in the second iteration.
To fix this, redo your iteration in this manner, remove your second foreach
as you do not need to iterate over the fields:
foreach ($field as $field_key => $item) {
if (is_array($item)) {
if ($field_key == 'join_two_fields') {
$body .= $this->$field_key($field1 = $item['field1'], $field2 = $item['field2']);
} else {
$body .= $this->$field_key($item);
}
} else {
$body .= $this->$item();
}
}
Hope this helps.
There are some issues that I noticed, like the one I mentioned in my comment above, however the biggest issue I notice is the way you are naming your parameters. If Im not mistaken, this does not match what you are supposed to be receiving in your function.
This is what I mean:
You currently pass parameters to the get_display_create_panel
method this way:
$display_panel_create->get_display_create_panel(
$size=6,
$title='Guest',
$action='Create',
$type='success',
$field=['email', 'phone', 'company','address', 'comments',
'join_two_fields' => [
'field1' => 'first_name',
'field2' => 'last_name'],
]
);
This means that get_display_create_panel()
should expect something like:
get_display_create_panel($size, $title, $action, $type, $field)
But you have it like this:
get_display_create_panel($size, $title, $field, $action, $type)
Which is inconsistent as it does not match. Make those match and that should fix your main issue.
you should change if ($k == 'join_two_fields')
to if ($k === 'join_two_fields')
because $k == 'join_two_fields'
is true when $k= 0
so test.php will work as below
$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>'; }
}
</div>