We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4018
    • 1,131 Posts
    Ok...got a small problem I’m trying to work out with a snippet I’m developing. I’m making use of the getDocumentChildrenTVarOutput call to include the pagename, id, and template variable for a range of pages. This particular snippet will be used to show a list of pages that belong to a specific category based on a provided template variable. However, the problem I’m having is finding an easy way to filter the returned getDocumentChildrenTVarOutput with just the pages that equal a certain value in the template variable. For example, let’s take the following code:

    $result = $modx->getDocumentChildrenTVarOutput($rootFolder,array("id","pagetitle","MyTVvar"),1,$order);


    The MyTVvar is just an example, but assume that the values of this array are numbers representing a specific category. Now, let’s say we only want pages where the MyTVvar equals 2....what’s the easiest way to filter out the $result array to where it only contains pages where the MyTVvar equals 2? That’s the problem. I’ve looked into the user of the array_filter function...but I’m not sure if it’s the right thing to use in this case. Any ideas?

    I’m almost beginning to think that maybe a small addition to these functions is necessary. Perhaps a change in the getDocumentChildrenTVarOutput and similar functions that allows for filtering might be handy.
      Jeff Whitfield

      "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
    • The array filter works great and is how we’ve modified to only work with children marked Show in Menu. Here’s an example that I hope helps. Array filters are my favorite new toy!

      function filterHidden($var) {
      	return (!$var['hidemenu']==1);
      }
      
      // filter out the content that is set to be hidden from menu snippets
      $result = array_filter($result, "filterHidden");
      
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 4018
        • 1,131 Posts
        Damn, you’re good! Yeah, it was the return line of the function that had me stumped! Think I got it now. smiley
          Jeff Whitfield

          "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
        • LOL... no problem... don’t blame me though, I’m just the messenger. wink
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 4018
            • 1,131 Posts
            I’ve been playing with the function you posted...but for some stupid reason, it doesn’t wanna pick up on a variable. For instance, I declared a variable with $catid at the top of my code. Then, using the array_filter call, I used a similar function like this:

            function filterHidden($var) {
            	return (!$var['hidemenu']==$catid);
            }
            


            If I replace the $catid variable with a hard-coded number, no problems. But for some reason the function doesn’t wanna pick up on the variable. Even if I declare the variable with a "global $catid" in the function itself...nope! Ugg!
              Jeff Whitfield

              "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
              • 4018
              • 1,131 Posts
              Well...I’m about to reach brainfry with this! It appears that I’m having mixed results with this. Try this on for size:

              Create a test snippet with the following code:

              $testvar = 17;
              
              $result = $modx->getDocumentChildrenTVarOutput(0,array("id","pagetitle"),1,$order);
              
              function filterCat($var) {
                   return ($var['id']!=3);
              }
              
              // filter out the content that does not equal the category
              $result = array_filter($result, "filterCat");
              reset($result);
              for($x=0;$x<count($result);$x++){
                   $testprint .= $result[$x]['id']." = ".$result[$x]['pagetitle']."<br />";
              }
              return $testprint;
              


              Now...insert this into an uncached test page and play with it a bit. Change the $var[’id’] in the fiterCat function to different ID’s of your documents. Also, try changing the != in the fiterCat function to == and run the test page again. Heck, try changing the ID number in the filterCat function to the $testvar and play with that a bit.

              Are you noticing some weird stuff like I am? I’ve tried this rather simple bit of code on two different installations...same results! Different things happen though. If the return string in the function is set to != then at times I get blank output variable for a page ID and pagetitle on the output. If I set it to ==, it’s pretty much blank all the time. Not to mention that I can’t seem to get the $testvar to work at all in the function. This is really weird! I may have to forgo the use of the array_filter function since it appears to be way too unpredictable. I can’t make heads or tails of it! Very weird!
                Jeff Whitfield

                "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
              • Have you tried with all uncached pages?
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 4018
                  • 1,131 Posts
                  Quote from: rthrash at Sep 01, 2005, 06:16 AM

                  Have you tried with all uncached pages?


                  Yep! Makes no difference. I tried this on my own site, another test site on the same server (clean install), and a clean install of MODx on my Linux server at home (to rule out any problems with my hosting provider). All three exhibited the same quirks. This is just strange.
                    Jeff Whitfield

                    "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                    • 4018
                    • 1,131 Posts
                    Ironically, this seems to work much better within a snippet:

                    $result = $modx->getDocumentChildrenTVarOutput(0,array("id","pagetitle"),1,$order);
                    
                    // filter out the content that does not equal the category
                    $testvar = 20;
                    
                    function filterCat($var){
                    global $testvar;
                    return $var["id"] != $testvar;
                    }
                    
                    $result = array_values(array_filter($result,'filterCat'));
                    
                    reset($result);
                    for($x=0;$x<count($result);$x++){
                         $testprint .= $result[$x]['id']." = ".$result[$x]['pagetitle']."<br />";
                    }
                    return $testprint;
                    


                    Adding the array_values function seems necessary since it reindexes the array. However, I still can’t get the function to see the $testvar variable. If you replace the $testvar in the return statement with a document ID number then it works quite well. So...last little quirk to work out!
                      Jeff Whitfield

                      "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                      • 4018
                      • 1,131 Posts
                      I’ll be damned! I tell you...if I had a ’real’ brain I would be really dangerous! Know what the problem was with the global variable? Simple! Just had to add another global $testvar to the beginning of the script...variable scope after all is pretty important. wink
                        Jeff Whitfield

                        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."