Accurax Posted January 16, 2007 Share Posted January 16, 2007 I think this might be a simple(ish) question... but here go'sI have a search form that allows people to select the genre of the music they are searching for, this is done a sa set of tick box's so they can return results of more than one genre at a time.However, my database only has one genre field for each member, and each member has selected only one genre for themselves.So how do i go about telling my search query to pull all the selected genres from the database?This problem is compunded by the fact that im building my search querey by putting all my $_POST values into a foreach statement, allowing me to exclude those that are intentionally left empty.Any idea's?Or havnt i been clear enough? Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/ Share on other sites More sharing options...
Cep Posted January 16, 2007 Share Posted January 16, 2007 I'd post your code Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161873 Share on other sites More sharing options...
Accurax Posted January 16, 2007 Author Share Posted January 16, 2007 This is the best i can explain it... the statements im talking about here are only part of a larger search query im builing, and i havnt mentioned them to avoid confusing things.Well, this is how im assigning the values gathered from the tick box's at the moment[code]$_POST['rock'] = "genre";$_POST['punk'] = "genre";$_POST['classic'] = "genre";[/code]Now, im assigning the $key to be equal to the selections made in the search form, and im assigning the $value to be qual to the name of the field in my mysql database.In order to allow the user to select one, all or none of the options above i run it through a foreach loop to create my querey.[code]<?phpforeach( $_POST as $key => $value) if(!empty($_POST[$k])) { $query.=" && `$v`='%$k%'";}else { $query.="";}?>[/code]The first value will be "hardcoded" into the query so theta the leading &&'s are not a problem, and will be the only search criteria that is required to be full.The query is then built as follows;[code]<?phpSELECT * FROM members WHERE[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161879 Share on other sites More sharing options...
Accurax Posted January 16, 2007 Author Share Posted January 16, 2007 wtf... i wasnt finished... grrr sorry guys it wont let me edit the post above.. i posted it by accident too early.. so ill retype and finish below Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161881 Share on other sites More sharing options...
Accurax Posted January 16, 2007 Author Share Posted January 16, 2007 This is the best i can explain it... the statements im talking about here are only part of a larger search query im builing, and i havnt mentioned them to avoid confusing things.Well, this is how im assigning the values gathered from the tick box's at the moment[code]$_POST['rock'] = "genre";$_POST['punk'] = "genre";$_POST['classic'] = "genre";[/code]Now, im assigning the $key to be equal to the selections made in the search form, and im assigning the $value to be qual to the name of the field in my mysql database.In order to allow the user to select one, all or none of the options above i run it through a foreach loop to create my querey.[code]<?phpforeach( $_POST as $key => $value) if(!empty($_POST[$k])) { $query.=" && `$v`='%$k%'";}else { $query.="";}?>[/code]The first value will be "hardcoded" into the query so theta the leading &&'s are not a problem, and will be the only search criteria that is required to be full.The query is then built as follows;[code]<?php"SELECT * FROM members WHERE location = '$loc' $query";[/code]So as you can see the problem is how i get it to take three input search fields and treat them as one field when querying the database.does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161884 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 OK, the first thing I'd do is change the form...Make all the check boxes have the same name e.g.<input type="checkbox" name="genre[]" value="rock"><input type="checkbox" name="genre[]" value="punk"><input type="checkbox" name="genre[]" value="classic">This way, once your form is submitted to php, $_POST['genre'] will be an array of genres and that's easier to deal with, it means that if nothing's checked then nothing's passed.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161887 Share on other sites More sharing options...
Accurax Posted January 16, 2007 Author Share Posted January 16, 2007 So id have an array inside an array right? Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161897 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 Yes, that's correct.So to access it you'd use something like this...[code]<?phpforeach($_POST['genre'] as $g){ $ "$g<br>\n";}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161915 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 Sorry, that was meant to read...[code]<?phpforeach($_POST['genre'] as $g){ echo "$g<br>\n";}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34398-joining-_post-variables/#findComment-161916 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.