is there an easier method to write search class than this code that i think it isn't
Hi all
i have written this search class, i want user to be able to search by title or category or company , or any
combination of these fields, e.g if user puts category and title, get search by those only, or if user puts
category and date he gets results be these fields only and so on, i have used in my class several if and elseif statments with all of possibilties, i just want to be sure if this is the optimal way, i think i can some thing more optimum than this, here is my class
totally untested, but should give you the general idea. (Note the use of double "$$" for "variable variables", which required me to change some of the function parameters to match the corresponding field names.)
PHP Code:
function jobs_by_title_category_company(
$job_title,
$category_id,
$company_title,
$job_date,
$salary_start,
$salary_end
)
{
$where = array();
$equalFields = array('job_title', 'category_id', 'company_name', 'date');
foreach($equalFields as $fld) {
if(!empty($$fld)) {
$where[] = $fld.'='.mysql_real_escape_string($$fld)."\n";
}
}
if(!empty($salary_start) and !empty($salary_end)) {
$where[] = 'job_salary>='.mysql_real_escape_string($salary_start).' AND '.
'job_salary<='.mysql_real_escape_string($salary_end)."\n";
}
$sql = "<everything before the where clause here, then...>
WHERE ".implode(' AND ', $where)."
<everything after the where clause here>";
// rest of function...
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks