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 have been working on a system for tracking vacation days, and I'm currently trying to make it so that within an edit page I can use checkboxes to select table rows (the checkboxes values = the mysql table row id's) and then delete them.

    I have it working so that I can select a single row and have it be removed using this snippit. Which you guys helped me with before.

    <?php
    $path = MODX_CORE_PATH . 'components/track/';
    $result = $modx->addPackage('track',$path . 'model/','pto_');
    
    $id = $_POST["1"]; 
     
    
     
    // Get the object we want to delete
    $obj = $modx->getObject('Track',$id);
    // Check if we actually retrieved an object to prevent fatal errors
    $success = true;
    
    if ($obj instanceof Track) {
    // if it's the right object, remove it. Will return true or false
      $success = $obj->remove();
    }
    			
    return $success;


    I've been trying to pass the checkbox values into an array, and then do a foreach loop but I keep getting an parsing error.

    I've tried it two ways, and both give me errors.

    #1 is
    <?php
    $path = MODX_CORE_PATH . 'components/track/';
    $result = $modx->addPackage('track',$path . 'model/','pto_');
    
    foreach($_POST["1"] as $deleterow => $id) {
    
    $deleterow = $modx->getObject('Track',$id);
      
     $success = true;
    
         if ($deleterow instanceof Track) {
    // if it's the right object, remove it. Will return true or false
      $success = $obj->remove();
    }
    }
    return $success;


    and #2 is:
    <?php
    $path = MODX_CORE_PATH . 'components/track/';
    $result = $modx->addPackage('track',$path . 'model/','pto_');
    
    $id = $_POST["1"]; 
     
    // 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
    $objs = $modx->getObject('Track',array("1",$id));
    // Check if we actually retrieved an object to prevent fatal errors
    $success = true;
    			 foreach(objs as obj) {
    if ($obj instanceof Track) {
    // if it's the right object, remove it. Will return true or false
      $success = $obj->remove();
    }
    			 }
    return $success;


    Any suggestions?
      • 3749
      • 24,544 Posts
      First, try this to see what you're getting:

      return '<pre>' . print_r($_POST, true) . '</pre>';


      Second, remove the quotes in $_POST["1"'] or (better) use actual name of the $_POST variable.


        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting