![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Greetings: I am attempting to split an input string using the str_word_count function to return multiple single word query variables. I can create an array based on $search=(str_word_count($search, 1, '0123456789')); (I want words and/or numbers). I can extract named variables from the resulting array via $search=extract($search, EXTR_PREFIX_ALL, search); and then call each variable by $search_0 $search_1 $search_2, etc. Several issues that I am having: Since the size of the array is determined by user input. I can't hardcode $search_x, $search_y, etc. because I don't know what x, y, etc. will be. How can I produce $search_x... et al. and then hand it/them to a query string? I can echo desired output using for or while loops, (i.e. the *value* of $search_x...et al. but I can't figure out how to get them into query strings. I want my query strings to contain something like $q=" SELECT * FROM * WHERE table1.row1 LIKE '%$search_x%' OR LIKE '%$search_y%' etc..." Advice or a sanity check is appreciated. |
#3
| |||
| |||
|
|
Jeff Gardner wrote: Greetings: I am attempting to split an input string using the str_word_count function to return multiple single word query variables. I can create an array based on $search=(str_word_count($search, 1, '0123456789')); (I want words and/or numbers). I can extract named variables from the resulting array via $search=extract($search, EXTR_PREFIX_ALL, search); and then call each variable by $search_0 $search_1 $search_2, etc. Several issues that I am having: Since the size of the array is determined by user input. I can't hardcode $search_x, $search_y, etc. because I don't know what x, y, etc. will be. How can I produce $search_x... et al. and then hand it/them to a query string? I can echo desired output using for or while loops, (i.e. the *value* of $search_x...et al. but I can't figure out how to get them into query strings. I want my query strings to contain something like $q=" SELECT * FROM * WHERE table1.row1 LIKE '%$search_x%' OR LIKE '%$search_y%' etc..." Advice or a sanity check is appreciated. Don't bother extracting. Just build your string in a foreach loop, i.e. $str = ''; foreach ($search as $s) { if ($str != '') $str .= ' OR '; $.str .= "LIKE $s"; } $sql = "SELECT * FROM table1 WHERE row1 $str"; This is what ended up working: |
![]() |
| Thread Tools | |
| Display Modes | |
| |