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

    I am hoping someone could help me out with a snippet I have written? Basically the snippet is working great (thanks to Shaun http://rtfm.modx.com/display/revolution20/Templating+Your+Snippets and Bob http://bobsguides.com/custom-db-tables.html). It returns all the info I need from a custom table and allows you to add a template and rowClass as parameters.

    The question I have is how to alternate the rowClass? I would need to add another parameter to the snippet call like
    &altClass=`someClass`


    My Snippet Code:
    <?php
    // add package so xpdo can be used:
    $path = MODX_CORE_PATH . 'components/its/';
    $result = $modx->addPackage('its',$path . 'model/','modx_');
    
    //$isbn = parameter value passed from snippet call
    
    $rows = $modx->getCollection('ITS',array('isbn'=>$isbn));
    $tpl = $modx->getOption('rowTpl',$scriptProperties,'itsRowTpl');
    $rowClass = $modx->getOption('rowClass',$scriptProperties,'brown');
    
    $output .= '<p>Total: '. count($rows) . '</p>';
    
    foreach($rows as $row) {
       $resourceArray = $row->toArray();
       $resourceArray['rowClass'] = $rowClass;
       $output .= $modx->getChunk($tpl,$resourceArray);
    }
    
    return $output;


    I know this is probably more of a php question but any help or advice would be great!

    Many Thanks

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

      Find me on Twitter, GitHub or Google+
    • discuss.answer
      • 31946
      • 116 Posts
      Try add this smiley

      $altClass = $modx->getOption('altClass',$scriptProperties,'yellow');
      $i = 0;
      foreach($rows as $row) {
         if($i % 2 == 0){
           $class = $rowClass;
         }else{
           $class = $altClass;
         }
         $resourceArray = $row->toArray();
         $resourceArray['rowClass'] = $class;
         $output .= $modx->getChunk($tpl,$resourceArray);
         $i++;
      }
        • 44234
        • 219 Posts
        Thanks ever so much Wanze, that's exactly what I needed! Thanks again
          Find me on Twitter, GitHub or Google+