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

    i created a player card (name, age, picture, points) with migx and can fetch the results to get in a ranking table (sort by by points) . For the rank i use the idx value. On some pages i want to show only one or some of the playercards and the problem is that idx always starts at "1" so for a single playercard the rank would also be 1 which is wrong. Is there a way to solve this with migx without adding another formfield "rank" where you have to change the rank manualy everytime the points change?

    Best,

    Mike
      • 4172
      • 5,888 Posts
      Its of course possible to prepare the json with a additional rank-field with an extra-snippet and put the result to getImageList as value, some like:

      [[getImageList? 
        &value=`[[prepareRank? &json=`[[*yourMIGXtv]]`]]`
        &tpl=`yourTpl`
        &where=`{"name":"theName"}`
      ]] 


      It would also be possible to prepare the rank-field with a plugin, which runs OnDocFormSave

        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 40737
        • 32 Posts
        Thanks Bruno,

        in the end i came up with that

            [[!getImageList?
            &value=`[[prodGetRank? &varname=`[[*longtitle]]` &id=`79`]]`
        	&tpl=`prodDieFahrerTpl`
        	]]


        prodGetRank:

        <?php
        
        
        // get Resource 
        $resource = $modx->getObject('modResource', $id);
        // get TV 
        $strJSON = $resource->getTVValue('migx3');
        // to Array 
        $arrJSON = $modx->fromJSON($strJSON);
        
        // prepare for multisort 
        foreach ($arrJSON as $key => &$row) {
            $punkte[$key]    = $row['punkte'];
            $name[$key] = $row['fahrername'];
        }
        
        // sort by "punkte"
        
        array_multisort($punkte, SORT_DESC, $arrJSON);
        
        // fill sorted array with rang
        $counter = 1;
        foreach ($arrJSON as $key => &$value) {
        	    $value['rang'] = $counter;
        		$counter++;
        		}
        
        // search function
        function search($array, $key, $value)
        {
            $results = array();
            if (is_array($array)) {
                if (isset($array[$key]) && $array[$key] == $value) {
                    $results[] = $array;
                }
                foreach ($array as $subarray) {
                    $results = array_merge($results, search($subarray, $key, $value));
                }
            }
            return $results;
        }
        
        // search varname
        $ergebnis = (search($arrJSON, 'MIGX_id', $varname));
        
        // to JSON 
        return json_encode($ergebnis);
        


        Dirty but it's doing the job.

        Thx!

        Mike