str_replace() JSON output?

str_replace() JSON output?

Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

Is there anyway to add the str_replace function to a JSON output whilst working with the Editor PHP server script? I need to substitute certain values from my 'checkcode' column and thought I could use some kind of str_replace function but I cannot seem to make it work?

// DataTables PHP library
include( "lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Validate;

Editor::inst( $db, 'cms_module_tps_userlisting', 'list_id' )
    ->fields(
        Field::inst( 'listphone' ),
        Field::inst( 'create_date' ),
        Field::inst( 'checkcode' ),
        Field::inst( 'user_id' )
    )
    ->where( 'user_id', $_GET['user_id'] )
        ->where( 'checkcode', $_GET['checkcode'] )
    ->process( $_POST )
    ->json();

Many thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,227Questions: 1Answers: 10,416 Site admin
    Answer ✓

    Sure, use a getFormatter. Something like:

    Field::inst( 'checkcode' )
      ->getFormatter( function ( $val ) {
        return str_replace( 'whatever', 'this', $val );
      } )
    

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2
    edited October 2014

    Thanks Allan, that worked really well.

    As you seem to be very adept at using datatables I don't suppose you can point me in the right direction of where I can find documentation illustrating how to add a class to my rows matching that of the value found in

    Field::inst ( 'checkcode')
    

    If my checkcode value is 'error' I can create a CSS class .error which will enable me to give the row a red background color, or something like that.

    Is that possible?

    Thanks

    Chris

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    I have added a class directly in my str_replace, is this a good thing to do though or is there a better way?

    Editor::inst( $db, 'cms_module_tps_userlisting', 'list_id' )
        ->fields(
            Field::inst( 'listphone' ),
            Field::inst( 'create_date' ),
            Field::inst( 'checkcode' )
            ->getFormatter( function ( $val ) {
            $code = array('EDNP', 'ENUM', 'VALID');
            $real = array('ERROR', '<span class="yellow">INCORRECT FORMAT</span>', '<span class="green">VALID</span>');
                return str_replace( $code, $real, $val );
            }),
            Field::inst( 'user_id' )
        )
    
  • allanallan Posts: 63,227Questions: 1Answers: 10,416 Site admin

    an point me in the right direction of where I can find documentation illustrating how to add a class to my rows matching that of the value found

    Use columns.createdCell, or possibly rowCallback.

    I have added a class directly in my str_replace, is this a good thing to do though or is there a better way?

    That looks fine to me, as long as your Editor can those values.

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    Thanks, seems to work well. However, I have another str_replace issue. This doesn't seem to work and I thought it would?

            Field::inst( 'registered_with_tps' )
            ->getFormatter( function ( $val ) {
            $null = array('0000-00-00 00:00:00');
            $placeholder = array('-');
                return str_replace( $null, $placeholder, $val );
            })
            ->getFormatter( 'Format::date_sql_to_format', 'd/m/Y \a\t H:i' )
    

    I don't want to display any dates which are 'null', i.e. 0000-00-00 00:00:00, so I replace them with a - symbol. That works fine.

    I also want to change the date format which works fine.

    However, if I combine the two ->getFormatter functions, the str_replace function doesn't run.

  • allanallan Posts: 63,227Questions: 1Answers: 10,416 Site admin

    You can't have two different formatter functions - you would need to combine the two operations into a single one.

    The code for the date_sql_to_format() method is available here.

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    HI Allan,

    Thanks for your continued support. I managed to get a working solution using the following if else statement within the getFormatter function.

            Field::inst( 'registered_with_tps' )
                    ->getFormatter( function ($val, $data, $field) {
                    if ($val == '0000-00-00 00:00:00') return "-";
                    else if ($val <> '0000-00-00 00:00:00') return $val = date('d/m/Y \a\t H:i');
                    })
    

    Chris

This discussion has been closed.