mysql - PHP - How to build multiple input search -
i have form around 7 inputs , want let user search according criteria choosea.
so example: inputs name can be:
- name
- age
- gender
- registration date
but user wants pick first 3, sql query like:
select * table name='.$name.' , age= '.$age.' , gender = '.$gender.'
cool work, facing problem of how build different sql query based on user input using mvc architecture.
my idea like:
// syntax nette framework please don't care it, basicly need logic in mvc public function findmatch($name= null, $age= null, $gender = null) { $selection = $this->database->table('table'); if ($name) { $selection = $selection->where('name', $name); } if ($age) { $selection = $selection->where('age', $age); } if ($gender) { $selection = $selection->where('gender', $gender); } return $selection; }
but helps "or" selection not "and". please can me figure out how solve this?
if considering single sql select
statement, might want try:
select * table t (@name null or t.name '%' + @name + '%') , (@age null or t.age = @age) ...
this search base on given criteria. basically, on where
clause, tells that:
if @name
null
- it display records name
- else, return values similar name only
Comments
Post a Comment