We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I'm glad you found something that worked. smiley
      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
      • 36924 ☆ A M B ☆
      • 298 Posts
      Nothing goes to waste: I just used the snippet to collate another set of resources where the gallery snippet call was not required. Fantastic!

      I attempted turning the pagetitles to link with this:
      $url = $modx->makeUrl($doc->get('id'), "", "", "full");
      but that threw an error stating I could not use get() for a non-object?

      $output .= '<h2><strong><a href="' . $url . '">' . $resource->get('pagetitle') . '</a></strong></h2>' .  $resource->get('content');
        • 3749
        • 24,544 Posts
        I can't see all the code, but you want to use either $resource or $doc in all cases. Only one of them is set.
          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
          • 36924 ☆ A M B ☆
          • 298 Posts
          The GroupByCategory Snippet:

          <?php
          $tvId = 30; /* change this to the ID of the category TV */
            
          function getDocs($category, $tvId) {
              global $modx;
              $tvrs = $modx->getCollection('modTemplateVarResource', array(
                  'tmplvarid' => $tvId,
                  'value'     => $category
              ));
              $docs = array();
              foreach ($tvrs as $tvr) {
                  $docs[] = $tvr->get('contentid');
              }
            
              $c = $modx->newQuery('modResource');
              $c->where(
                array('id:in' => $docs)
              );
              $c->sortby('menuindex', 'ASC'); 
          
           
            
              $resources = $modx->getCollection('modResource', $c);
          
              $url = $modx->makeUrl($doc->get('id'), "", "", "full");
          
           
              $output = '<div class="someclasses"><div class="someclasses2"><p>' . $category . '</p>';
           
              foreach ($resources as $resource) {
           
           
          /*start formatting output */
          $output .= '<h2><strong><a href="' . $url . '">' . $resource->get('pagetitle') . '</a></strong></h2>' .  $resource->get('content');
           
          /*end formatting output */
           
              }
          
          
          $output .= '</div></div>';
            
              return $output;
          }
            
          /* Get all possible categories */
          $c = $modx->newQuery('modTemplateVarResource');
          $c->distinct();
          $c->where(array('tmplvarid' => $tvId));
            
          $tvrs = $modx->getCollection('modTemplateVarResource', $c);
            
          $output = '';
          /* Get the resources in each category */
          $usedCategories = array();
          foreach ($tvrs as $tvr) {
              $category = $tvr->get('value');
              if (! in_array($category, $usedCategories)) {
                  $usedCategories[] = $category;
                  $output .= getDocs($category, $tvId);
                 }
              }
            
          return $output;

            • 3749
            • 24,544 Posts
            The code will skip any TVs that are set to the TV's default value, but offhand, I don't see anything wrong with the code (assuming that 30 is really the ID of the TV).

            Do you see anything in the error log?
              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
              • 36924 ☆ A M B ☆
              • 298 Posts
              The code works but when I decide to turn the PageTitles into links (by setting the $url in line 25 and using it in 34) I get this error:

               Fatal error: Call to a member function get() on a non-object in C:\wamp\www\nestnew\ore\cache\includes\elements\modsnippet\46.include.cache.php on line 28


              but if in line 34 I replace $url with $resource->get('url') there are no errors but the URL is wrongly inserted as the siteurl (i.e. http://localhost/siteDir) [ed. note: ojchris last edited this post 12 years, 3 months ago.]
                • 3749
                • 24,544 Posts
                That's because $docs is an array of IDs not a resource object.

                Move the line inside the foreach ($resources as $resource) loop and make it:

                $url = $modx->makeUrl($resource->get('id'), "", "", "full");
                  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
                  • 36924 ☆ A M B ☆
                  • 298 Posts
                  That did it. Bob I'm learning from this help. Thanks.
                    • 3749
                    • 24,544 Posts
                    I'm glad you got it working. smiley
                      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
                      • 36924 ☆ A M B ☆
                      • 298 Posts
                      I'll like to sortby the category TV, what changes should I make to this
                      $c->sortby('menuindex', 'ASC');