Jump to content

Recommended Posts

I think this might be a simple(ish) question... but here go's

I 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?
Link to comment
https://forums.phpfreaks.com/topic/34398-joining-_post-variables/
Share on other sites

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]<?php
foreach( $_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[/code]
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]<?php
foreach( $_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?
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.

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