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);
$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);
}
$imgName = $family->get('name');$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);
} Looks great.Thanks so much Bob!!
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); }