We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    Quote from: BobRay at May 11, 2015, 02:05 PM
    I didn't notice the calculations.

    I would do something like this rather than array_merge(), it should be slightly faster and I think the code is a little easier to follow:

    
    $fields = $family->toArray();
    $imgName = $family->get('name');
    $fields['nwN'] = 'Name: ' . $imgName;
    $fields['imgLink'] = 'assets/media/jpgs/' . $imgName . '.jpg';
    $output .= $modx->getChunk('showProductFam', $fields);


    For sorting:

    $c = $modx->newQuery('BbProducts');
    $c->where(array('fam' => (int) $famId));
    $c->sortby('name', 'ASC');
    $families = $modx->getCollection('BbProducts', $c);

    This works beautifully!! Thanks very much!

    This is my resulting code:
    $famId = $_GET['famCode'];
    
    $c = $modx->newQuery('BbProducts');
    $c->where(array('fam' => (int) $famId));
    $c->sortby('name', 'ASC');
    
    $families = $modx->getCollection('BbProducts', $c);
    foreach($families as $family) {
        $fields = $family->toArray();
        $imgName = $family->get('name');
        $fields['nwN'] = 'Name: ' . $imgName;
        
        $fields['noImage'] ='assets/media/jpgs/noimage-s.jpg';
        $imgName = $family->get('name');
        $fields['imgLink'] = 'assets/media/jpgs/' . $imgName . '.jpg';
        if(!file_exists($fields['imgLink'])){
          $fields['imgLink'] = $fields['noImage'];
        }
    
        $output .= $modx->getChunk('showProductFam', $fields);
    }
    • discuss.answer
      • 3749
      • 24,544 Posts
      Looks great. smiley

      No need to do this twice, though:

       $imgName = $family->get('name');


      Also, If there's no '+noImage' placeholder in the Tpl chunk, this will be more efficient (moves setting the value for noImage outside the loop, since it never changes; and avoids several associative array references which are much slower than variable references):

      $noImage = 'assets/media/jpgs/noimage-s.jpg';
      
      foreach($families as $family) {
          $fields = $family->toArray();
          $imgName = $family->get('name');
          $fields['nwN'] = 'Name: ' . $imgName;
          $temp = 'assets/media/jpgs/' . $imgName . '.jpg';
          $fields['imgLink'] = file_exists($temp) ? $temp : $noImage;
          $output .= $modx->getChunk('showProductFam', $fields);
      } 
        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
        • 45118
        • 123 Posts
        Quote from: BobRay at May 12, 2015, 07:03 PM
        Looks great. smiley

        No need to do this twice, though:

         $imgName = $family->get('name');


        Also, If there's no '+noImage' placeholder in the Tpl chunk, this will be more efficient (moves setting the value for noImage outside the loop, since it never changes; and avoids several associative array references which are much slower than variable references):

        $noImage = 'assets/media/jpgs/noimage-s.jpg';
        
        foreach($families as $family) {
            $fields = $family->toArray();
            $imgName = $family->get('name');
            $fields['nwN'] = 'Name: ' . $imgName;
            $temp = 'assets/media/jpgs/' . $imgName . '.jpg';
            $fields['imgLink'] = file_exists($temp) ? $temp : $noImage;
            $output .= $modx->getChunk('showProductFam', $fields);
        } 
        Thanks so much Bob!!
          • 3749
          • 24,544 Posts
          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