limitphp Posted November 18, 2008 Share Posted November 18, 2008 I have a query statement: $queryVote = "SELECT songName WHERE genre='$genre' "; $resultVote = mysql_query($queryVote) or die (mysql_error()); I have a form (checkboxes) that lets users select whatever genres they want. So, technically, I won't know which genres they select. There are alot of genres, but to simplify things, lets just say there are four genres. Rock, Jazz, Folk and Blues. Lets say a user chooses only three (Rock, Jazz and Blues). Is there a way to make that query work with whatever genres they select? Even if they select mutliple genres? It would really be tedious to have to write: if ($genre=='Jazz'){ $query = } if ($genre=='Rock'){ $query = } if ($genre=='Jazz' && 'Rock'){ $query = } Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/ Share on other sites More sharing options...
.josh Posted November 18, 2008 Share Posted November 18, 2008 dunno what the rest of your code looks like, so you may have to alter this a bit, but basic idea is implode the checkbox array to make a comma/quote separated string of the selected genres and then use the IN() comparison function: $genres = implode("','", $_POST['genre']); $query = "SELECT songName FROM tablename WHERE genre IN('$genres')"; echoing $query should for instance look like this: SELECT songName FROM tablename WHERE genre IN('Jazz','Rock','Blues') Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-692962 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 $genres = implode("','", $_POST['genre']); $query = "SELECT songName FROM tablename WHERE genre IN('$genres')"; [/code] echoing $query should for instance look like this: SELECT songName FROM tablename WHERE genre IN('Jazz','Rock','Blues') So, a query can handle multiple values if you use the IN? Right now, my genre checkboxes all have different names. Like rock, jazz, etc. Should I name them all the same thing and just do that implode to have php automatically put a comma between them? The only problem this would cause is with some javascript I have. I have a checkbox named allRock, that when you click it checks all the rock genres. if (document.frmname.allRock.checked){ document.frmname.rock.checked=true; document.frmname.indie.checked=true; etc... } If they were all named the same thing, then I couldn't refer to them by name in the javascript. But, maybe I can refer to them by their value. And I can put a this() int he input tag. <input type=checkbox name=genre value=allGenres onClick='javascript:submit(this.form)> ???? if (document.frmname.genre.value(allGenre).checked??????){ document.frmname.genre.value(folk).checked=true; } If I can't get the javascript to work, then I can just create the $genre list inside a genre variable with if statements. I'll hopefully test the query out tommorrow. If it works, that is going to be awesome.... Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693140 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 I would assume that selecting multiple values for a field would be somewhat common.... ex) Select * FROM tbl WHERE city = houston, dallas, austin or SELECT * FROM tbl WHERE color = red, blue, white Would all those queries require this IN? Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693143 Share on other sites More sharing options...
blueman378 Posted November 19, 2008 Share Posted November 19, 2008 could you not load them into an array and then use foreach? Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693149 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 could you not load them into an array and then use foreach? I don't understand. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693162 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 could you not load them into an array and then use foreach? Are you refering ro helping with the javascript or the query? Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693479 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 Ok, I have the javascript working correctly now. I just replaced the reference with the name to using elements. document.frmGenres.genre[0].checked=true; etc... But the code $genres = implode("','", $_POST['genres']); Is giving me errors. It says invalid arguments being passed to implode. Does anyone know why? Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693501 Share on other sites More sharing options...
.josh Posted November 19, 2008 Share Posted November 19, 2008 are you naming your checkboxes as an array called genres? example <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693509 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 are you naming your checkboxes as an array called genres? example <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> No. They are all named genre. <input type = 'checkbox' name = 'genre' value='whatever'> <input type = 'checkbox' name = 'genre' value='whatever'> So, name them : <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> Do I need numbers in the boxes like: <input type = 'checkbox' name = 'genres[0]' value='whatever'> <input type = 'checkbox' name = 'genres[1]' value='whatever'> Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693512 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 are you naming your checkboxes as an array called genres? example <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> No. They are all named genre. <input type = 'checkbox' name = 'genre' value='whatever'> <input type = 'checkbox' name = 'genre' value='whatever'> So, name them : <input type = 'checkbox' name = 'genres[]' value='whatever'> <input type = 'checkbox' name = 'genres[]' value='whatever'> Do I need numbers in the boxes like: <input type = 'checkbox' name = 'genres[0]' value='whatever'> <input type = 'checkbox' name = 'genres[1]' value='whatever'> No you do not need numbers in the boxes. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693513 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 Ok, I changed the names to have the genre[] Its still giving me the same error: for this code: $genres = implode("','", $_POST['genres']); invalid arguments passed to implode. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693519 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 Are you sure the form is being processed first? I would honestly do a check before implode: if (isset($_POST['genres']) && is_array($_POST['genres'])) { $genres = implode("','", $_POST['genres']); }elseif (isset($_POST['genres'])) { echo 'Genres was not an array for some reason. }else { echo 'Genres was not passed in through post.'; } Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693522 Share on other sites More sharing options...
Philip Posted November 19, 2008 Share Posted November 19, 2008 You said the form name is "genre[]", but you're still putting in $_POST['genres']... or is that just a typo in your post? Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693525 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 You said the form name is "genre[]", but you're still putting in $_POST['genres']... or is that just a typo in your post? No, thats a typo. I changed it to genre. I think the problem is the extra single quotes. it works like this EXAMPLE 1: $genres = implode(",", $_POST['genre']); But not like this EXAMPLE 2: $genres = implode("','", $_POST['genre']); Problem is, in example 1, it returns Jazz,Rock,Blues I need it to return it with single quotes around it like 'Jazz','Rock','Blues' Also, even though example 1 works, if no genre is selected than I get that invalid arguments passed to implode error. I guess I can take care of that by giving genres a default value. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693530 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 $genres = "'" . implode("', '", $_POST['genres']) . "'"; Try that, after I tested it, I noticed it neglected the first and last single quotes. That should work. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693543 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 $genres = "'" . implode("', '", $_POST['genres']) . "'"; Try that, after I tested it, I noticed it neglected the first and last single quotes. That should work. Ok, that worked! It now returns: 'Rock' or if multiple are selected: 'rock','jazz','blues' Awesome! Now, does anyone know how to grab an input with a name of genre[]? <input type=checkbox name=genre[] value=rock /> My code of if (document.frmGenres.allrock.checked==false){ document.frmGenres.genre[0].checked=false; document.frmGenres.genre[1].checked=false; document.frmGenres.genre[2].checked=false; document.frmGenres.genre[3].checked=false; } No longer works. Should I put ID tags in the checkbox tags and use that? If so, how do you grab a form item using the ID? Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693595 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 I think you have to reference it by an index. Not 100% sure. Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it's numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example: variable = documents.forms[0].elements['var[]']; http://ca3.php.net/manual/en/faq.html.php#faq.html.select-multiple A tidbit of information. Here is an example of using JS to reference that type of input http://www.kirupa.com/forum/showthread.php?t=282632 Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693602 Share on other sites More sharing options...
.josh Posted November 19, 2008 Share Posted November 19, 2008 or you can go ahead and put the numbers in the [] you don't have to for the php implode to work, but you don't not have to, either. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693611 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 nevermind....I got it. I just added ID=genre and the javascript now works. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693615 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 or you can go ahead and put the numbers in the [] you don't have to for the php implode to work, but you don't not have to, either. I actually tried that and it didn't work. But adding an ID=genre for all checkboxes worked. Thanks guys! I really do appreciate all the help! Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693617 Share on other sites More sharing options...
.josh Posted November 19, 2008 Share Posted November 19, 2008 well it should have worked. Implode only cares that the argument is an array. It doesn't matter what the array keys are. So if it "didn't work" then it was something else. Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693639 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 well it should have worked. Implode only cares that the argument is an array. It doesn't matter what the array keys are. So if it "didn't work" then it was something else. I'm sorry, I meant the javascript didn't work. The implode worked fine. Sorry for the confusion. When I switched to <input type=checkbox name=genre[0] value=rock /> javascript no longer worked document.frmGenres.genre[0].checked=false; the implode worked great.... once I added an ID <input ID=genre type=checkbox name=genre[0] value=rock /> the javascript started working again.... document.frmGenres.genre[0].checked=false; Quote Link to comment https://forums.phpfreaks.com/topic/133240-solved-query-multiple-choices-within-one-field/#findComment-693656 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.