<div>[[+total]] Products</div> <form action="[[~[[*id]]]]" method="POST"> <p>Choose a Group or Groups:</p> <div id="check"> <input type="checkbox" name="group[]" id="check1" value="BRA" /> <label for="check1">Brackets</label> <input type="checkbox" name="group[]" id="check2" value="BLK" /> <label for="check2">Blocks</label> <input type="checkbox" name="group[]" id="check3" value="COR" /> <label for="check3">Corbels</label> <input type="checkbox" name="group[]" id="check4" value="GBR" /> <label for="check4">Gingerbreads</label> <input type="submit" name="submit" Value="Submit"/> </div> </form>
<?php
$output = "";
$path = MODX_CORE_PATH . 'components/aeibbproducts/';
$result = $modx->addPackage('aeibbproducts',$path .
'model/','aei_');
if (! $result) {
return 'failed to add package';
}
if (isset($_POST['group']) && (! empty($_POST['group']))) {
$group = $_POST['group'];
}
$c = $modx->newQuery('BbProducts');
foreach($group as $selected) {
$c->where(array('pre' => $selected));
}
$total = $modx->getCount('BbProducts',$c);
$totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
$modx->setPlaceholder($totalVar,$total);
$modx->runSnippet('FeetInch');
$fi = new FeetInch($modx);
$noImage ='assets/media/jpgs/noimage-s.jpg';
$products = $modx->getCollection('BbProducts',$c);
foreach($products as $product) {
$fields = $product->toArray();
$width = $product->get('width');
$fields['nwW'] = 'Width: ' . $fi->dec2inch($width);
$height = $product->get('height');
$fields['nwH'] = 'Height: ' . $fi->dec2inch($height);
$proj = $product->get('proj');
$fields['nwP'] = 'Proj: ' . $fi->dec2inch($proj);
$pitch = $product->get('pitch');
$fields['nwPi'] = $pitch . ' Pitch';
$fleName = $product->get('aei');
$temp = 'assets/media/jpgs/prod-s/' . '01-' . $fleName . '.jpg';
$fields['imgLink'] = file_exists($temp) ? $temp : $noImage;
$output .= $modx->getChunk('showBrackets', $fields);
}
return $output;This question has been answered by multiple community members. See the first response.
foreach($group as $selected) {
$c->where(array('pre' => $selected));
}$where = array();
foreach($group as $selected) {
$where[] = array('pre' => $selected);
}
$query->where($where, xPDOQuery::SQL_OR);This code is resetting the where array to the last match:
$i = 0
foreach($group as $selected) {
$condition = ($i) ? 'OR:' : '';
$c->where(array($condition.'pre' => $selected));
$i++;
}
You want something like this (untested):
$where = array(); foreach($group as $selected) { $where[] = array('pre' => $selected); } $query->where($where, xPDOQuery::SQL_OR);
$i = 0 foreach($group as $selected) { $condition = ($i) ? 'OR:' : ''; $c->where(array($condition.'pre' => $selected)); $i++; }
This should work too (untested).
if (isset($_POST['group']) && (! empty($_POST['group']))) {
$group = $_POST['group'];
} else {
$group = array();
}
The error log says "PHP Parse error: syntax error, unexpected 'foreach' (T_FOREACH)".
That error usually means that at some point, it's being called where $group is not an array. What is the code that's setting the $group variable?Hi Bob, you're right! I didn't set the $group variable... I added this and no error message anymore!
$group = array();
That happens if no option is selected during a form post ($_POST['group'] is empty then)Hi Jako,
Should fix that.if (isset($_POST['group']) && (! empty($_POST['group']))) { $group = $_POST['group']; } else { $group = array(); }
$group = array();
if (isset($_POST['group']) && (! empty($_POST['group']))) {
$group = $_POST['group'];
}