We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21414
    • 41 Posts
    I am trying to be able to obtain the values from several select boxes. (the values are row id's from a database table).

    I want to take the selected check boxes values, search those values on the database, and delete those rows.

    This is the snippit i've started working on, but as i'm still just learning php with modx, this code isn't working, anyone have any suggestions on how I can achieve what i'm looking to do?

    <?php
    $path = MODX_CORE_PATH . 'components/pto/';
    $result = $modx->addPackage('pto',$path . 'model/','pto_');
    
    $from = $modx->getCollection('Tracker');
    // rowcheckbox is the name of my checkboxes, the value of each check box is the row id of the database row I want deleted
    $where = "id=" . $_GET['rowcheckbox'];
    
    $modx->db->delete($from,$where);
    
    return true;
    
      • 22303 MODX Staff
      • 10,725 Posts
      There is no $modx->db available in MODX Revolution 2.1+. You can remove a single row using the removeObject() method that modX inherits from xPDO:

      <?php
      $path = MODX_CORE_PATH . 'components/pto/';
      $result = $modx->addPackage('pto',$path . 'model/','pto_');
      
      $removed = $modx->removeObject('Tracker', array('id' => $_GET['rowcheckbox']));
      
      return $removed ? '' : 'There was an error removing row with id ' . $_GET['rowcheckbox'];
        • 18373 ☆ A M B ☆
        • 3,141 Posts
        Okay, let's make one thing clear first. Are you using MODX Evolution or Revolution?

        You're using Revo syntax (addPackage, getCollection) and also $modx->db which is Evo..

        If you're trying to delete whatever is passed in a GET request, do realize that's asking for trouble smiley


        Now, I'll assume you're on Revo and set up your package properly..
        <?php
        $path = MODX_CORE_PATH . 'components/pto/';
        $result = $modx->addPackage('pto',$path . 'model/','pto_');
        
        $id= $_GET['rowcheckbox'];
        
        // I assume that the rowcheckbox post is a SINGLE ID and not an array.. if so, you'll need to wrap this in a for each or something.
        
        // Get the object we want to delete
        $obj = $modx->getObject('Tracker',$id);
        // Check if we actually retrieved an object to prevent fatal errors
        $success = true;
        if ($obj instanceof Tracker) {
        // if it's the right object, remove it. Will return true or false
          $success = $obj->remove();
        }
        
        return $success;
        


          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • 21414
          • 41 Posts
          Thanks Guys,

          Yes I am using Revo, but didn't realize I was using evo code.

          I'll try this and see if I can get it to work.