We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    I have around sixty TVs whose values are numerical.

    On most pages I want to display these TV values as they are, without any modification, but on one of my pages I would like to substitute the numerical values for text.

    For example:
    tv1 value = 9
    tv2 value = 5
    tv3 value = 1

    tv1 displayed value = Daily activity
    tv2 value = 1 - 2 times yr
    tv3 value = Never

    I thought of using Output Modifiers for this but I can only apply one condition.

    I have written the following snippet for tv1 and it works, but it is just for one TV.

    I am worried that by the time I have replicated this for each of the sixty TVs the page will be incredibly slow to load.

    Does anyone have any suggestions for how I could arrive at a better solution that may be quicker to implement and a little less of a drain on the server resources?

    <?php
    if ($modx->resource->getTVValue('tv1') == 1) {
    
            $output = '<li>Never</li>';
    
        }
    
        elseif ($modx->resource->getTVValue('tv1') == 9) {
    
            $output = '<li>Daily activity</li>';
        }
    
        elseif ($modx->resource->getTVValue('tv1') == 8) {
    
            $output = '<li>Weekly activity</li>';
        }
    
        elseif ($modx->resource->getTVValue('tv1') == 7) {
    
            $output = '<li>Monthly activity</li>';
        }
    
        elseif ($modx->resource->getTVValue('tv1') == 6) {
    
            $output = '<li>3 - 6 times yr</li>';
        }
    
        elseif ($modx->resource->getTVValue('tv1') == 5) {
    
            $output = '<li>1 - 2 times yr</li>';
        }
    
        elseif ($modx->resource->getTVValue('tv1') == 4) {
    
            $output = '<li>Occasional</li>';
        }
        return $output;


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

      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
      • 4172
      • 5,888 Posts
      if this are listbox or radiooption - buttons, why not input-options like
      1==<li>Never</li>||9==<li>Daily activity</li>||8==<li>Weekly activity</li>||7==<li>Monthly activity</li>||6==<li>3 - 6 times yr</li>||5==<li>1 - 2 times yr</li>
      


      or have chunks with names like

      activity_1, activity_9, activity_8

      with this contents and call them with

      [[$activity_[[*tv1]]]]



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

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • discuss.answer
        • 24629
        • 370 Posts
        Mmmm maybe i'm getting you wrong but when you make a custom modifier (snippet) you can use it on any tv you want. So make a snippet with a switch statement and call it 'myModifier'
         switch ($input) {
            case 1 :
                $output = '<li>Never</li>';
                break;
            case 9 :
                $output = '<li>Daily activity</li>';
                break;
            case 8 :
                $output = '<li>Weekly activity</li>';
                break;
            case 7 :
                $output = '<li>Monthly activity</li>';
                break;
            case 6 :
                $output = '<li>3 - 6 times yr</li>';
                break;
            case 5 :
                $output = '<li>1 - 2 times yr</li>';
                break;
            case 1 :
                $output = '<li>Occasional</li>';
            default:
                $output = 'no rule found';
        }
        return $output;


        on the page in question use
        tv1 displayed value = [[*tv1:myModifier]]
        tv2 displayed value = [[*tv2:myModifier]]
        tv3 displayed value = [[*tv3:myModifier]]

        I haven't tested this code but something along those lines should work.
        RDG
          • 38783
          • 571 Posts
          Quote from: Bruno17 at Aug 02, 2017, 10:39 PM
          if this are listbox or radiooption - buttons, why not input-options like
          1==<li>Never</li>||9==<li>Daily activity</li>||8==<li>Weekly activity</li>||7==<li>Monthly activity</li>||6==<li>3 - 6 times yr</li>||5==<li>1 - 2 times yr</li>
          


          or have chunks with names like

          activity_1, activity_9, activity_8

          with this contents and call them with

          [[$activity_[[*tv1]]]]





          Hello Bruno,

          Thank you for looking at this for me.

          I am using input options of the type you have suggested. So the end user, who is updating their data in NewsPublisher sees the phrase, not the number that is being stored.

          On some pages the data is returned in tables where the stored numerical values are what I want to display.

          On other pages I want to display the data as the phrases like Never, Weekly activity etc.
            If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

            email: [email protected] | website: https://andytough.com
            • 38783
            • 571 Posts
            Quote from: rdaneeel at Aug 03, 2017, 02:28 AM
            Mmmm maybe i'm getting you wrong but when you make a custom modifier (snippet) you can use it on any tv you want. So make a snippet with a switch statement and call it 'myModifier'
             switch ($input) {
                case 1 :
                    $output = '<li>Never</li>';
                    break;
                case 9 :
                    $output = '<li>Daily activity</li>';
                    break;
                case 8 :
                    $output = '<li>Weekly activity</li>';
                    break;
                case 7 :
                    $output = '<li>Monthly activity</li>';
                    break;
                case 6 :
                    $output = '<li>3 - 6 times yr</li>';
                    break;
                case 5 :
                    $output = '<li>1 - 2 times yr</li>';
                    break;
                case 1 :
                    $output = '<li>Occasional</li>';
                default:
                    $output = 'no rule found';
            }
            return $output;


            on the page in question use
            tv1 displayed value = [[*tv1:myModifier]]
            tv2 displayed value = [[*tv2:myModifier]]
            tv3 displayed value = [[*tv3:myModifier]]

            I haven't tested this code but something along those lines should work.
            RDG

            Hello Ralph

            Thank you. This is working perfectly and is going to save me a lot of work!

            In addition this solution has taught me some new php and clarified how custom output modifiers work.
              If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

              email: [email protected] | website: https://andytough.com