We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22608
    • 2 Posts
    Any help appreciated...

    I am using getResources (modx revolution, most updated version of getResources) to sort an array of template variables that are numbers in descending order. It works fine except for when the template variable becomes a double digit number. It returns an array listing 9 down and then the double digit number.

    [[!getResources? &parents=`4` &depth=`2` &limit=`3` &tpl=`homeGoalsChunk` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &sortbyTV=`Goals` &sortdirTV=`DESC` ]]

    I am using it to list the top three "Goal" scorers for a team. getResources skips the top scorer with 11 goals, and then lists the next top three with 7,4, and 4 respectively. If I limit the array to the whole team (30 records) then the person with 11 goals is listed just above those players with 1 goal.

      • 22303 MODX Staff
      • 10,725 Posts
      TV values are always strings. If you want to sort on it, you’ll have to make it a sortable string, i.e. prepend 0 for as many digits as you need.
        • 22608
        • 2 Posts
        Thank you Jason for your response. Do you happen to have a rough example so I can see where this is done? Do I have to set the template variable output to string? Do I use the tv prefix attribute of getResources? Thank you very much for your time and help.
          • 20865
          • 30 Posts
          TV values are always strings. If you want to sort on it, you’ll have to make it a sortable string, i.e. prepend 0 for as many digits as you need.
          Please give an example or describe how to do this, it isn’t clear how to do this.
            • 31912 MODX Staff
            • 95 Posts
            I haven’t tried it, but I think he is saying that if someone scored one goal it should be entered as 01 instead of 1 since the values are being sorted as a string rather than numeric value. If you plan on needing up-to 999, a player with one goal would need to be entered as 001.
              • 29019 ☆ A M B ☆
              • 104 Posts
              If you’re doing it that way, you may want to tack a 0 on the other end also, so you have 0010, 0020, etc. That way if you have to move 0030 between 0010 and 0020, you can make it 0015 and not have to change 0020 (and anything after it).
                www.darkstardesign.com

                Are you in the Nashville area? Join us for our next MODX meetup!
                • 19534
                • 183 Posts
                Quote from: palmps at Jan 05, 2011, 10:00 AM

                Any help appreciated...

                I am using getResources (modx revolution, most updated version of getResources) to sort an array of template variables that are numbers in descending order. It works fine except for when the template variable becomes a double digit number. It returns an array listing 9 down and then the double digit number.

                [[!getResources? &parents=`4` &depth=`2` &limit=`3` &tpl=`homeGoalsChunk` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &sortbyTV=`Goals` &sortdirTV=`DESC` ]]

                I am using it to list the top three "Goal" scorers for a team. getResources skips the top scorer with 11 goals, and then lists the next top three with 7,4, and 4 respectively. If I limit the array to the whole team (30 records) then the person with 11 goals is listed just above those players with 1 goal.



                You might try to cast the varchar field to a number implicitly by adding zero:

                &sortbyTV=`Goals+0`

                I don’t know if that works with getResource (haven’t looked deeper into the code which performs the sorting) but when working with SQL this normal works...
                  Add-On to easily manage your multilingual sites: Babel
                  • 20865
                  • 30 Posts
                  Thanks to all for respondes.
                  I have done this with $rate = str_pad($rate, ’6’,’0’,STR_PAD_LEFT); in my snippet that makes rating and $rate = ltrim($rate,"0") that gets rating and outputs it without zero.
                    • 17384
                    • 24 Posts
                    Quote from: Nadoedalo at Feb 04, 2011, 02:32 AM

                    Thanks to all for respondes.
                    I have done this with $rate = str_pad($rate, ’6’,’0’,STR_PAD_LEFT); in my snippet that makes rating and $rate = ltrim($rate,"0") that gets rating and outputs it without zero.

                    I have the same problem, but i dont know where to do that.

                    Thanks.
                      • 20865
                      • 30 Posts
                      Quote from: logiag at Feb 12, 2011, 11:17 AM

                      Quote from: Nadoedalo at Feb 04, 2011, 02:32 AM

                      Thanks to all for respondes.
                      I have done this with $rate = str_pad($rate, ’6’,’0’,STR_PAD_LEFT); in my snippet that makes rating and $rate = ltrim($rate,"0") that gets rating and outputs it without zero.

                      I have the same problem, but i dont know where to do that.

                      Thanks.
                      I have a snippet that fills tv.Rating when $_GET was isset(and many other checks). $rate is selected from a `modx_site_tmplvar_contentvalues` and then calculated. After all manipulations and checks :
                        if($rate > 0){$rate = str_pad($rate, '6','0',STR_PAD_LEFT);} // It is working wrong when outputting
                       $modx->db->query("INSERT INTO modx_site_tmplvar_contentvalues(tmplvarid,contentid,value) VALUES ('".tmplvarid"','".$id."','".$rate."');");
                      

                      And output
                      if($number != 0)
                          $number = ltrim($number,"0");
                      echo $number<0 ? "<font color='red'>".$number."</font>" : "<font color='green'>".$number."</font>";​

                      Calling:
                      [[!checkNumber? &number=`[[+tv.Rating]]`]]