We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37557
    • 25 Posts
    I'm trying to query the database if an entry exists to decide if an object can be deleted.
    This code is ugly since I'm a noob programmer.

     if ($delete) {
            /*Fetching authorname */
            $delauthor = $modx->getObject($DB_Author, array('id'=>$delete));
            $delauthorname = $delauthor->get('author');
           
            /* query DB_Store if a book is assigned to the author to prevent deletion */
            if ( $books = $modx->getObject($DB_Store, array(
                   'authorFirst' => $delauthorname,
            ))) {
              $output .= ('<div style="color: #ff1493; font-size: 2.2em; margin: 200 0 0 200px; width: 300px;">author can't be removed because there are assigned books to him/her</div>');
            } else {
              /*we're deleting */
              if ( $delauthor->remove()) { /*exception handling */
                $output .= ('<div style="color: #ff1493; font-size: 2.2em; margin: 200 0 0 200px; width: 300px;">Success! Author was removed</div>');
              } else {
                $output .= ('<div style="color: #ff1493; font-size: 2.2em; margin: 200 0 0 200px; width: 300px;">Arggh! Something went wrong. Probably the form was not filled out proper</div>');
              } /*end execption handling*/ 
            }
          } 


    Why isn't this working the xpdo rtfm says if no object is retrieved its null. The error is in the equation
    Parse error: syntax error, unexpected T_STRING in ... . This is probably quite simple but I can't figure it out. I'm using revo.
    Thank you for help
    dreh
      • 37557
      • 25 Posts
      Autsch. This works. Sorry for the noise.
       if ($delete) {
              /*Fetching authorname */
              $delauthor = $modx->getObject($DB_Author, array('id'=>$delete));
              $delauthorname = $delauthor->get('author');
             
              /* query DB_Store if a book is assigned to the author to prevent deletion */
      
              if ( $modx->getObject($DB_Store, array(
                     'authorFirst' => $delauthorname,
              ))) {
                $output .= ('<div style="color: #ff1493; font-size: 2.2em; margin: 200 0 0 200px; width: 300px;">author cant be removed because there are assigned books to him/her</div>');
              } else {
                /*we're deleting */
                if ( $delauthor->remove()) { /*exception handling */
                  $output .= ('<div style="color: #ff1493; font-size: 2.2em; margin: 200 0 0 200px; width: 300px;">Success! Author was removed</div>');
                } else {
                  $output .= ('<div style="color: #ff1493; font-size: 2.2em; margin: 200 0 0 200px; width: 300px;">Arggh! Something went wrong. Probably the form was not filled out proper</div>');
                } /*end execption handling*/ 
              }
            }