Forum Moderators: coopster

Message Too Old, No Replies

Multiple Select form, Mysql research with PHP

Multiple Select form, Mysql research with PHP

         

karukera7

9:04 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Hello,

I want to create a search engine in a MYSQL database, where I have a form from which a user can enter a keyword, and choose more than 1 option in a Multiple choice select, like this :

Field to enter keywords :
<td style="vertical-align:middle; width:15%"><b>Enter Keywords</b> </td>
<td style="vertical-align:middle">
<input name="keywords" type="text" value="" maxlength="500" id="keywords"/></td>

Multiple choice select to choose many options :

<select size="8" name="category[]" multiple id="category" style="width:180px;">
<option selected="selected" value="ALL">All-----</option>
<option value="Accountants">Accounting</option>
<option value="Advertising/Marketing">
Advertising/Marketing</option>
<option value="Banking/Finance">Banking/Finance</option>
<option value="Construction">Construction</option>
<option value="Courier">Courier</option>
</select>

I want to retrieve datas from the multiple select field and with the keywords entered previously, I want to make a research in the database. My problem is that I have one field in my table in the data base named "Category".
If a user type a keyword and choose many options in the category multiple select field from the form , I don't know how to build a MYSQL command with PHP in order to see, that for each option chosen from the multiple select field and the keyword, which rows corrrespond to these selected options. Because a user can choose 5 options or 10 options or more in the multiple select field. If think it should a dynamic select.

If you could give me an hand that would be nice.

Thank you.

eelixduppy

7:05 pm on Nov 28, 2007 (gmt 0)



Something like this should work. This uses the IN [dev.mysql.com] function which is very powerful when used correctly.

$categories = array_map("mysql_real_escape_string",$_POST['category']); #escape each element in the array
$comma_sep = "'".[url=http://www.php.net/implode]implode[/url](',',$categories)."'";
$query = "SELECT * FROM table WHERE category IN (".$comma_sep.") AND etc...";

A thread you might also find helpful: [webmasterworld.com...]