• Custom snippet, alternating row classes [Solved]#

  • romanum Reply #1, 4 months ago

    Reply
    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


  • Wanze Reply #2, 4 months ago

    Reply
    Try add this
    $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++;
    }


  • romanum Reply #3, 4 months ago

    Reply
    Thanks ever so much Wanze, that's exactly what I needed! Thanks again