We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50574
    • 70 Posts
    Can I utilise the output mods when calling a TV to ask how many checkboxes were selected in the TV to then affect what is shown? For example:
    [[+tv.tvName:isequalto=`1`:then=`<p>insert text:</p>`:else=`<p>insert this text</p>`]]
    At the moment I think this is just counting how many times the tvName is being called on the page. The TV has input options calling a chunk if selected:
    option1==[[$option1Chunk?]]||option2==[[$option2Chunk?]]
    so I'm not sure if that affects what can be done or if I can have two 'values' for each option, a count and the chunk?

    Thanks in advance
      • 4172
      • 5,888 Posts
      untested snippet 'count_options':

      <?php
      
      $tvname = $modx->getOption('tvname', $scriptProperties, '');
      $docid = $modx->getOption('docid', $scriptProperties, $modx->resource->get('id'));
      
      $value = '';
      $classname = 'modTemplateVar';
      if ($object = $modx->getObject($classname, array('name' => $tvname))) {
          $tvid = $object->get('id');
          $classname = 'modTemplateVarResource';
          if ($object = $modx->getObject($classname, array('contentid' => $docid, 'tmplvarid' => $tvid))) {
              $value = $object->get('value');
          }
      }
      
      $values = !empty($value) ? explode('||',$value) : array();
      
      return count($values);
      


      inside getResources-tpl:
      [[count_options:is=`1`:then=`<p>insert text:</p>`:else=`<p>insert this text</p>`? &docid=`[[+id]]` &tvname=`yourtvname`]]
      [ed. note: Bruno17 last edited this post 8 years, 8 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 50574
        • 70 Posts
        Hi Bruno many thanks for the reply.
        As I'm still getting to grips with ModX I'm a little unsure what the above is doing.

        I've created the snippet and changed the 'tvname' in the first line:
        <?php
         
        $tvname = $modx->getOption('stockists', $scriptProperties, '');
        $docid = $modx->getOption('docid', $scriptProperties, $modx->rescource->get('id'));
        $value = '';
        $classname = 'modTemplateVar';
        if ($object = $modx->getObject($classname, array('name' => $tvname))) {
            $tvid = $object->get('id');
            $classname = 'modTemplateVarResource';
            if ($object = $modx->getObject($classname, array('contentid' => $docid, 'tmplvarid' => $tvid))) {
                $value = $object->get('value');
            }
        }
        $values = !empty($value) ? explode('||',$value) : array();
        return count($values);

        and am calling it in a chunk (productBoxStockist):
        <div class="stockistListBox">
        [[count_options:is=`1`:then=`<p>insert text:</p>`:else=`<p>insert this text</p>`? &docid=`[[+id]]` &tvname=`stockists`]]
                    <ul>
                        [[+tv.stockists]]
                    </ul>
                </div>

        which is then called in the resource as a getResource:
        [[!getResources? &parents=`2` &depth=`1` &limit=`0` &tpl=`productBoxStockist` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`1` &sortby=`menuindex` &sortDIR=`DESC`]]

        It currently shows nothing on the page, even removes the all the page content but I don't really know where to begin to look as there aren't an errors being shown in the snippet code.

        If you could either give me some more hints or point me in the direction of what I should research to help me figure it out I'd be most appreciative.
        • Usually if there is a blank page that means there is a fatal PHP error that isn't allowing MODX to run at all, so there won't be any MODX-reported errors. Check your server's PHP error log.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 50574
            • 70 Posts
            Thanks Susan.

            The rest of the site works fine, just not the page that this is running on.
            Would that also be related to a fatal PHP error? [ed. note: cottoncreative last edited this post 8 years, 8 months ago.]
              • 3749
              • 24,544 Posts
              I'm not sure if this is your problem, but you've misspelled 'resource'. That would definitely kill the snippet.

              $modx->rescource->get('id')


              BTW, I couldn't see anything wrong with your snippet, but I pasted your code into PhpStorm and it flagged the error because it knows the $modx object has no such field. My PhpStorm license has paid for itself *many* times over.

                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
                • 50574
                • 70 Posts
                Bob, you're a saint and thanks again to Bruno.

                For clarity the count_option snippet code is:
                <?php
                $tvname = $modx->getOption('tvname', $scriptProperties, '');
                $docid = $modx->getOption('docid', $scriptProperties, $modx->resource->get('id'));
                $value = '';
                $classname = 'modTemplateVar';
                if ($object = $modx->getObject($classname, array('name' => $tvname))) {
                    $tvid = $object->get('id');
                    $classname = 'modTemplateVarResource';
                    if ($object = $modx->getObject($classname, array('contentid' => $docid, 'tmplvarid' => $tvid))) {
                        $value = $object->get('value');
                    }
                }
                $values = !empty($value) ?explode('||',$value) : array();
                return count($values);
                  • 3749
                  • 24,544 Posts
                  Thanks. 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