We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37946
    • 70 Posts
    I am making an ajax call to this script:

     $output = $modx->runsnippet('getResources', array(
            'debug' => '1',
            'parents' => '1',
            'where' => '{"pagetitle:=":'.$location.'}',
            'tpl' => 'locationTpl',
            'default' => '<p>No results found</p>'
        ));
    


    All I know is the parent id. I will not know the id of the actual resource so I am trying to get a resource by pagetitle. Just not having much luck. Thank you for your time!
      • 3749
      • 24,544 Posts
      If you just want a single resource, using getResources is serious overkill.

      You might try something like this:

      $resource = $modx->getObject('modResource', array('pagetitle' => $location));
      
      if ($resource) {
         $fields = $resource->toArray();
         $output = $modx->getChunk('locationTpl', $fields);
      } else {
         $output = '<p>No results found</p>';
      }


      If you need to restrict it to resources directly under resource 1, it would be:

      $resource = $modx->getObject('modResource', array('pagetitle' => $location, 'parent' => '1'));
        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
        • 37946
        • 70 Posts
        HOLY CRAP THIS IS AWESOME!
        I knew getResources was a bit much, just wasn't sure of another way. Yes, I'll only need 1 resource at any given time. This is brilliant!
        Thank you Bob!!
          • 3749
          • 24,544 Posts
          Glad I could help, but don't thank me until it works. wink

            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
            • 37946
            • 70 Posts
            Oh, it will work. smiley I'll post my completed code for future reference. Thanks again!
              • 37946
              • 70 Posts
              I did end up using getResources in conjunction with your suggestion. Works perfectly. I am doing a LOT of assumption IMO by comparing pagetitle. I would like to come up with a better more fool-proof method of getting the resource other than by pagetitle. I also need to go back and clean up/sanitize the $_POST data to reduce any unwanted data coming across.

              Here is the code for anyone else who finds this post.

              Javascript:
              $.ajax({
                  type: 'post',
                  url: 'my-url-path',
                  data: {'location': myLocationParam},
                  dataType: 'html',
                  timeout: 50000
              }).done(function(locationResponse) {
                  if (locationResponse !== 'fail')
                  {
                      // Handle success.
                      console.log('----------------- LOCATION OUTPUT -----------------');
                      console.log(locationResponse);
                      console.log('---------------------------------------------------');
                  }
                  else // No location data.
                  {
                      // Handle no data.
                  }
              }).fail(function(error) {
                  // Handle fail.
              });
              


              PHP:
              if ( isset($_POST['location']) )
              {
                  $location = $_POST['location'];
                  $resource = $modx->getObject('modResource', array('pagetitle' => $location, 'parent' => 1)); // Limit search to resource ID 1
              
                  if ( $resource )
                  {
                      $fields = $resource->toArray(); // An arrry of page fields (pagetitle, longtitle etc).
              
                      $resoruceID = $fields['id'];
                      $headline = $fields['pagetitle'];
              
                      $output = $modx->runsnippet('getResources', array(
                          'parents' => $resoruceID ,
                          'tpl' => 'myTemplateTpl',
                          'showHidden' => 1,
                          'default' => '<p>No data found for this location.</p>'
                      ));
                  }
                  else
                  {
                      $output = '<p>No results found!</p>';
                  }
              }
              else // No data to make getResources call.
              {
                  $output = 'fail';
              }
              
              echo $output;
              


              Chunk (myTemplateTpl) - (for example purpose):
              <h1>[[+longtitle]]</h1>
              [[+introtext]]
              
                • 3749
                • 24,544 Posts
                Ideally, you would use the resource ID, which unlike the pagetitle, can never change. If that would work, you'd do something like this:

                $output = '';
                $children = array();
                $parent = $modx->getObject('modResource', $location);
                
                
                if ($parent) {
                   $children = $parent->getMany('Children');
                }
                
                if (!empty($children)) {
                    $output .= "Headline: " . $parent->get('pagetitle');
                    foreach($children as $child) {
                        $fields = $child->toArray();
                        $output .= $modx->getChunk('myTemplateTpl', $fields;
                    }
                } else {
                    $output = '<p>No results found!</p>';
                }
                
                echo $output;
                
                  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