⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callback_field - Trouble with Parameter Passing !?



jeanjunker
  • profile picture
  • Member

Posted 02 September 2017 - 12:03 PM

I need to create several callbacks for field formatting and need to do this dynamically as I don't know the exact FieldNames until runtime.

I can make everything in GC work except in the callback_field (or callback_edit or callback_add)

 

 I think I just don't know PHP well enough to know how to get this coded right so it works.

 

Below is a simple hand-coded example.

What I need is for the VALUE of the $ded1 and $ded2 variables to be INSERTED into the input statement INSIDE the function.

In the sample code where you see ... name="'.$ded1.'" ... I need it to evaluate to ... name="Compression" ... for $ded1 and name="Marketing" for $ded2, etc.

 

Somehow because of the "function" Declaration, those two variables are not in scope and I can't seem to get this going?

The first 1/2 of each callback_field where the $ded1 and $ded2 are coded works fine, its the 2nd part where the "function" is declared is where it breaks.

 

What am I missing?

I am basically trying to do either this or to create the two callback functions dynamically in code, but I can't figure out how to do that either...

 

Any feedback appreciated.

Jean.

 

EXAMPLE CODE:

 

$ded1 = 'Compression';

$crud->callback_field($ded1, function ($value, $primary_key) {
return '<input type="numeric" maxlength="15" value="'.$value.'" name="'.$ded1.'" style="width:100px">';
});
$ded2 = 'Marketing';
$crud->callback_field($ded2, function ($value, $primary_key) {
return '<input type="numeric" maxlength="15" value="'.$value.'" name="'.$ded2.'" style="width:100px">';
});


 


jeanjunker
  • profile picture
  • Member

Posted 02 September 2017 - 13:34 PM

I found a work-around by using the "create_function" function that seems to work...

 

However, this create_function is Deprecated in 7.2.0 so I don't know what that means exactly, will it still work later?

 

I also read that one may have memory/garbage collection issues by doing this, If there is a better way I would like to hear about it.

So far, this is the only way that I can do what I need:

 

$ded = 'Compression';

$value = 777.99;
$funcbody = 'return \'<input type="numeric" maxlength="15" value="'.$value.'" name="'.$ded.'" style="width:100px">\';';

$crud->callback_field($ded, create_function ('$value, $primary_key', $funcbody));

#
$ded = 'Marketing';
$value = 888.99;
$funcbody = 'return \'<input type="numeric" maxlength="15" value="'.$value.'" name="'.$ded.'" style="width:100px">\';';
$crud->callback_field($ded, create_function ('$value, $primary_key', $funcbody));

 

This technique allows me to control / populate the both input field form VALUE and NAME which is what I needed to do...

Jean.