List Box: Multiple Select, search db by
I have a list box that allows for multiple select.
I would like to have the user search a database table based on what they select: as many as they select.
I am also working on a "select all" button too that would select everything inside the listbox...
Either way,
so far, I have read that I have to use a form with the multiple select box and then use the post method, and link the form to a results page.
I would then take the array of that multiple select box and incorporate it into a search.
An example: select box is called: man.
And it will post values 1 through 5 to results.php.
Buuuut, that's as far as I've gotten.
Can someone provide me with some links/references/tips on how to accomplish this so I can study up on it? Thanks.
Re: List Box: Multiple Select, search db by
a "select all" button can be achieved through javascript, like here.
as far as searching in a database goes, it's simple. if you have an array of items that are allowed to be returned, you simply add to the where clause. this will build your query:
PHP Code:
<?php
$terms = array("brown", "black", "white", "orange", "red"); //search terms
$sql = "SELECT * FROM dogs WHERE ";
for($i = 0; $i < count($terms); $i++){
$sql .= "color='{$terms[$i]}'";
if($i != (count($terms) - 1)) $sql .= " OR "; //add an OR unless we're on the last item
}
$sql .= " LIMIT 15";
echo $sql;
?>
this should also work when there's only one item selected.
Re: List Box: Multiple Select, search db by
Thanks, I will work on this.
Re: List Box: Multiple Select, search db by
I am getting the information from a form that I have on a previous page.
The form's list box is called: man
I have this:
$terms = array($_POST['man']);
However it only seems to select the last item selected for the search, it does not use the "OR" as needed.
Thanks.
I will work on it.
Re: List Box: Multiple Select, search db by
no no, you can't create an array like that. you need to set $terms to the value of $_POST['man'], not try to put it into an array of itself. you also might not have your form set up correctly -- "man" on your form needs to be named "man[]" so that it can hold multiple values. otherwise, you will only ever return one. I modified my code to include a test form to show you:
PHP Code:
<pre>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$terms = $_POST['colors']; //search terms
$sql = "SELECT * FROM dogs WHERE ";
for($i = 0; $i < count($terms); $i++){
$sql .= "color='{$terms[$i]}'";
if($i != (count($terms) - 1)) $sql .= " OR "; //add an OR unless we're on the last item
}
$sql .= " LIMIT 15";
echo $sql;
}
?>
</pre>
<form action="selectall.php" method="post">
<select name="colors[]" size="6" multiple>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="orange">Orange</option>
<option value="pink">Pink</option>
</select>
<input type="submit" value="Submit" />
</form>
Re: List Box: Multiple Select, search db by
Thank you kows, by the way, for your last post.
I appreciate it... As I mentioned in another thread, I am reconfiguring my database design while its just a baby, based on my website design.
I will post back with questions.
Thank you.