We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43592
    • 97 Posts
    Hi forum!

    I cannot sort by pagetitle:

    $resources[] = $res->get('pagetitle') . '==' .$res->get('pagetitle');
    $resources->sortby('pagetitle', 'ASC'); 
    


    2nd line doesnt work!
    Any ideas?

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      ->sortby only works in the context of a query to the database, and doesn't make sense when applied to the $resources array.

      We need to see more of your code in order to help you.
        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
        • 43592
        • 97 Posts
        As always Thank you Bob Ray! Here is the full code of the snippet:

        <?php
        //Spezielle Gruppe Fachtrainer
        $superusergroups = $modx->getOption('superusergroups', $scriptProperties,'8,9,11');
        
        //Kursinformationen von Parent Kursinformationen
        $parent = $modx->getOption('parent', $scriptProperties, 47);
        
        $superusergroups = explode(',', $superusergroups);
        
        $groups = $modx->user->getUserGroups();
        
        $has_permission = false;
        $accepted = false; //see below
        
        //Ist der User überhaupt in der Superusergroup?
        foreach ($superusergroups as $group) {
            if (in_array($group, $groups)) {
        
        	      $has_permission = true; //user is in one of the superusergroups
        
                  $gruppe = $group;
        /*	NEW: check which superuser group
        			if ($group[0] != "")
        			{
        			$gruppe = "1";
        			}
        			else if ($group[1] != "")
        			{
        			$gruppe = "2";
        			}
        			else
        			{
        			$gruppe = "3";
        			}
        
        
         $modx->log(modX::LOG_LEVEL_ERROR, $gruppe);
         $modx->trigger_error($gruppe, E_USER_WARNING);
         */
        	}
        	
        }
        $modx->log(modX::LOG_LEVEL_ERROR, $gruppe);
        //Überprüfen welche Gruppe
        
        
        $resources = array();
        if ($parentObj = $modx->getObject('modResource', $parent)) 
        {
         $c = $modx->newQuery('modResource');
         if ($resArray = $parentObj->getMany('Children', $c)) 
         {
            foreach ($resArray as $res) 
        	{
        
               // if ($res instanceof modResource) 
        	//	{
                    if ($res->get('description') == 'Impuls Lehrtrainer 2') 
        			{
                        if ($has_permission AND $gruppe > "10")
        				{
                            $accepted = true;
                        }
                    } 
        
        			else if ($res->get('description') == 'Impuls Lehrtrainer') 
        			{
                        if ($has_permission AND $gruppe > "8")
        				{
                            $accepted = true;
                        }
                    } 
        
        				else if ($res->get('description') == 'Fachtrainer') 
        			{
                        if ($has_permission AND $gruppe >= "8")
        				{
                            $accepted = true;
                        }
                    } 
        
        			else 
        			{
                        $accepted = true;
        
                    }
            //    }
        
                if ($accepted) 
        			{
                   // $resources[] = $res->get('pagetitle') . '==' .$res->get('id');
        		   $resources[] = $res->get('pagetitle') . '==' .$res->get('pagetitle');
        		   //$resources->sortby('pagetitle', 'ASC'); 
                	}
                $accepted = false; //reset accepted to false
              }
        }
        
        }
        $out = implode("||", $resources);
        
        return $out;
        
        • discuss.answer
          • 3749
          • 24,544 Posts
          Try adding this line just before line 100:

          natcasesort($resources);


          It would be faster if you refactored the whole operation so the resources you want could be pulled with a single query, using:

          $c->sortby('pagetitle', 'ASC');


          but your selection criteria are so complex that it would be a very difficult query.
            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
            • 43592
            • 97 Posts
            Thank you, that worked perfectly!!
              • 3749
              • 24,544 Posts
              I'm glad I could help. 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
                • 53655
                • 1 Posts
                code to fetch title and description for dynamic website