Using if statement within Editor PHP server script with $_GET

Using if statement within Editor PHP server script with $_GET

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

I am passing 2 parameters to my Editor PHP server script which are picked up using the $_GET function. It works well however, on occasion, one of the parameters I am passing will be empty in which case I want to discard that check. I cannot understand why the following if isset check code does not work though, can anyone point me in the right direction?

// Build our Editor instance and process the data coming from _POST 
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'] )
     if ( isset($_GET['checkcode']) && !empty($_GET['checkcode']) ) {   
         ->where( 'checkcode', $_GET['checkcode'] )
     }
     ->process( $_POST )
    ->json();

Thanks

Chris

Answers

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    edited January 2015

    Hi Chris,

    It looks like invalid PHP syntax to me, and you should probably be getting an error from the PHP engine. You want something like this:

    // Build our Editor instance and process the data coming from _POST
    $editor = 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'] );
    
    if ( isset($_GET['checkcode']) && !empty($_GET['checkcode']) ) {  
         $editor->where( 'checkcode', $_GET['checkcode'] );
    }
    
    $editor
         ->process( $_POST )
        ->json();
    

    Does that make sense now?

    Allan

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

    The didn't seem to do the trick but this did work.

    if(isset($_GET['checkcode']) && $_GET['checkcode']!=''){
    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();
    } else {
    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'] )
        ->process( $_POST )
        ->json();
    }
    

    Thanks for your help

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Fair enough - although I'm curious as to why my solution didn't work. Did you get any errors from it?

    However, good to hear you have a working solution!

    Allan

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

    Thanks for your help. I didn't see any errors although that didn't mean there weren't any! Thanks again.

  • GeorgeIoakGeorgeIoak Posts: 27Questions: 6Answers: 0

    Just to let you know, the reason your code didn't work is because it does throw a PHP error:

    FastCGI sent in stderr: "PHP message: PHP Parse error: syntax error, unexpected '}'

    You forgot a ; If you change to this it works

    $editor->where( 'checkcode', $_GET['checkcode'] );

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Doh - thanks for that. Clean missed it before! Updated the comment now incase anyone else sees it.

    Allan

This discussion has been closed.