How could i select all column from table in datatables editor?

How could i select all column from table in datatables editor?

Aarti SharmaAarti Sharma Posts: 8Questions: 5Answers: 0
edited August 2018 in Free community support

I want to retrieve all column of a table as i am creating columns dynamically .

Editor::inst( $db,$table,'ITEM' )
    ->fields(
        Field::inst( 'name' )..//instead if filling field name here want to retrieve all fields of table  
    )

    ->process( $_POST )
    ->json();

please help me if anyone have solution for that.

Thanks in advance

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Editor doesn't support that out of the box I'm afraid, but it is something that you can programmatically do using the API. The way to approach this is to execute a SHOW TABLE SQL command to get information about the table - you would then loop over the columns in the table and add a Field::inst() for each one.

    The downside to that approach is that it is more difficult to control validation of data. For example if you have a column for e-mail addresses it would be stored as plain text. You would need to find a way to add an e-mail address validator to it - possibly through the column name or possibly having another lookup table.

    Allan

  • Aarti SharmaAarti Sharma Posts: 8Questions: 5Answers: 0

    That will work for editor also?

  • Aarti SharmaAarti Sharma Posts: 8Questions: 5Answers: 0
    edited August 2018

    i am using for loop but its not allowing me to use it.

     Editor::inst( $db,$table,'ITEM' )
    ->fields(
    foreach($arr as $val){
       echo  "Field::inst( '$val' )"
            }
    )
    
    ->process( $_POST )
    ->json();
    

    Error is:
    Parse error: syntax error, unexpected 'foreach' (T_FOREACH)

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited August 2018

    Wrong post. Sorry.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Yes, the PHP being used there isn't valid. What you would do is something like:

    $editor = Editor::inst( $db,$table,'ITEM' );
    
    foreach( $arr as $val ) {
      $editor->field( Field::inst( $val ) );
    }
    
    $editor->process( $_POST );
    $editor->json();
    

    Allan

This discussion has been closed.