Customizing datatables php scripts on the fly

Customizing datatables php scripts on the fly

malpautimalpauti Posts: 8Questions: 4Answers: 1
edited August 2017 in Free community support

Datatables generator has created php scripts which I'm attempting to customize. The code segment below was created in table.applications.php and tells editor which fields to use

Editor::inst( $db, 'applications', 'appid' )
    ->fields(
    ->fields(
        Field::inst( 'appname' ),
        Field::inst( 'appdesc' )
    )
    ->process( $_POST )
    ->json();

I'm attempting to generate this code segment programmatically to allow me to easily adapt to changing table and form fields. So, I've attempted to iterate to create the field definitions using the following code:

$tablefields = array(
    "appname", 
    "appdesc"
);
$editor_php_construct ="";
$arrlength = count($tablefields);
for($x = 0; $x < $arrlength; $x++) {
    $editor_php_construct .= "Field::inst( '".$tablefields[$x]."')";
    if ($x+1 < $arrlength) {
        $editor_php_construct .= ",";
    } else {
        $editor_php_construct .= ")";
    }
}

Running this code gives me the equivalent of

        Field::inst( 'appname' ),
        Field::inst( 'appdesc' )

How can I incorporate $editor_php_construct into the script and evaluate it?

Thanks in advance for your help.

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    You wouldn't construct the string version of the PHP since you'd need to then evaluate that. I guess you could write it into a file and then PHP include it, but that would seem like a significant security risk and processing overhead to me.

    Instead simply do:

    $editor = new Editor( ... );
    
    for ( ... ) {
      $editor->field( new Field( ... ) );
    }
    

    Allan

  • malpautimalpauti Posts: 8Questions: 4Answers: 1

    Thank you so very much Alan. Really appreciate your responsiveness and expertise.

This discussion has been closed.