php - Dividing up multiword $_POST input -


i trying have php form takes in text input, queries underlying mysql table.

<form method="get" action="search.php" name ="searchbar">             <input type="text" name"search">             <input type="submit" value="search"> </form> 

the schemas of tables querying
movie(id, title, year, rating, company)
actor(id, last, first, sex, dob, dod)

for movie, think it'll easy because make query where title '%".$_post['search']."%' along line. actor, since name divided 2 parts (last, first), think need break down inputs words can compare each word attributes. (so if input 'hanks tom' or 'tom hanks', query able find actor tom hanks either input)

there way achieve this, or possibly smarter way approach this?

you have search every combination of input see if 1 of them match first or last name. keep in mind first , last reserved words mysql. can use following.

$input = 'tommy lee jones';  $names = explode(' ', preg_replace('/(\s)+/', ' ', $input);); $temp = array();  foreach($names $key) {     $temp[] = "`first` '%$key%'";     $temp[] = "`last` '%$key%'"; }  $s = ' , ( '.implode(' or ', $temp).' )';  echo $s; 

result:

and ( `first` '%tommy%' or `last` '%tommy%' or `first` '%lee%'  or `last` '%lee%' or `first` '%jones%' or `last` '%jones%' ) 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -