php - Search MySQL database with Filter options selected by the user. Database structure -
i have form user can fill out, , wish search mysql database entries user has selected. instance, if user enters keyword, , checks option 2 , 3, want search database keyword , options 2 , 3.
i'm not sure how set database however. need column in table each option, or there way store array within table? technique occurred me have separate table options.
i've seen done before, i'm not sure on proper execution. if knows proper way this, great.
edit: database of places, (say, music stores), , options different instruments sold. when user fills out form, want return list of stores names include 'keyword' string , sell instruments selected user.
i'm not sure how set database in order proper query. should have each option separate column boolean value, or can have column array of boolean values?
note: 'instruments' 1 of many options. want able filter other criteria
the html :
<form> <input type="text" name ="keyword" value="keyword"/><br/> option 1<input type="checkbox" name="option 1" value="option1"/><br/> option 2<input type="checkbox" name="option 2" value="option2"/><br/> option 3<input type="checkbox" name="option 3" value="option3"/><br/> option 4<input type="checkbox" name="option 4" value="option4"/><br/> <button type="submit">submit</button> </form>
proposed db structure:
id | store_name | guitar | piano | saxophone | cello | 1 mike\'s music true false false true
or:
id | store_name | instruments | 1 mike\'s music array[2]
proposed db structure:
stores id | store_name 1 mike\'s music instruments id | desc 1 guitar 2 piano 3 saxophone 4 cello stocks id | store_id | instrument_id 1 1 1 2 1 4
so query can :
$query = "select distinct store_name stores inner join stocks on stores.id = store_id 1 = 1 "; if ($keywords!="") { $query .= "and store_name concat('%','".$keywords."','%') "; } if ($option1!="") { $option[] = 1; } if ($option2!="") { $option[] = 2; } if ($option3!="") { $option[] = 3; } if ($option4!="") { $option[] = 4; } if (count($option)>0) { $query .= "and instrument_id in (".implode(",", $option).")"; }
Comments
Post a Comment