Jump to content

Recommended Posts

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...

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')

$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....

 

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?

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?

 

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'>

 

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'>

 

 

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.

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.';
}

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.

$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?

 

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

 

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!

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;

 

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.