If I understand you correctly, simply construct your where clause based on all the input. That is, if the user first selects GEM, then RED, then other stuff, your first query would have
WHERE type='gem'
and the second page would preselect item type 'gem' in the form, which means this information is retained when they select color 'RED'. The second query would have
type='gem' AND color='red'
just like it would have if they specified both "gem" and "red" at the the same time.
And it can all be easily handled by
# function to build (this part of) the where string
function filterString($filters, $data)
{
$parts = array();
foreach($filters as $f => $t)
{
if (!empty($data[$f]))
{
switch ($t)
{
case 'int':
$parts[] = sprintf('%s=%d', $f, $data[$f]);
break;
case 'string':
$parts[] = sprintf("%s='%s'", $f, $data[$f]);
break;
}
}
}
return ' ' . implode(' AND ', $parts) . ' ';
}
# your post data
$_POST = array(
'type' => 'GEM',
'size' => '4'
);
# settings providing the function with information of which information
# to use for filtering
$filter_vars = array('type' => 'string', 'color' => 'string', 'size' => 'int');
$where = filterString($filter_vars, $_POST);
echo $where;