php - filtering search results using multiple parameters from $_GET -
i working on faceted search scheme in php uses arguments $_get
filter search results. i'm having problem returning right search results. core of problem revolves around next (wrong) code:
$hits = results_from_db(); $results = array(); foreach ($hits $hit) { if (isset($_get['online']) && !in_array('online', $hit)){ continue; } elseif (isset($_get['on-site']) && !in_array('on-site', $hit)){ continue; } elseif (isset($_get['scheduled']) && !in_array('scheduled', $hit)){ continue; } elseif (isset($_get['on-demand']) && !in_array('on-demand', $hit)){ continue; } $results[] = $hit; } homecoming $results;
if user selects more 1 alternative - 'online'
, 'scheduled'
- $results
should contain of hits have 'online'
and/or 'scheduled'
inclusive. if user selects no options, of results should returned.
in above code, however, results won't homecoming if more 1 alternative selected.
i sense simple logic problem, i'm still stumped.
as per comment, sounds job database, here solution:
if( foreach ($hits $hit) { $matches = false; if (isset($_get['online']) && in_array('online', $hit)){ $matches = true; } elseif (isset($_get['on-site']) && in_array('on-site', $hit)){ $matches = true; } elseif (isset($_get['scheduled']) && in_array('scheduled', $hit)){ $matches = true; } elseif (isset($_get['on-demand']) && in_array('on-demand', $hit)){ $matches = true; } if($matches){ $results[] = $hit; } }
edit per comment, total solution:
$hits = results_from_db(); $categories = array( 'online', 'on-site', 'scheduled', 'on-demand' ); $optionselected = false; foreach ($categories $cat) { if(isset($_get[$cat])){ $optionselected = true; break; } } if(!$optionselected){ //return info homecoming $hits; } $results = array(); foreach ($hits $hit) { $matches = false; foreach ($categories $cat) { if (isset($_get[$cat]) && in_array($cat, $hit)){ $matches = true; break; } } if($matches){ $results[] = $hit; } } homecoming $results;
this more extended incorporate future categories adding $categories array
php
No comments:
Post a Comment