I've been trying to figure this out for a few days now, but haven't been able to find a solution. Most likely due to the fact that I don't think I'm asking the right question.
Here is goes...
I am trying to create a search on my website of a list of attorneys.
Table 1
ID (primary)
Name
Category1 (fkey)
Category2 (fkey)
Category3 (fkey)
Category4 (fkey)
Category5 (fkey)
Location1 (fkey)
Location2 (fkey)
Location3 (fkey)
Location4 (fkey)
Location5 (fkey)
Table 2 - Locations
ID (primary)
Name
Table 3 - Categories
ID (primary)
Name
So Attorneys have multiple categories and multiple locations -> one to Many Relationship with both Table 2 and Table 3
First Question: Do I have Table 1 set up correctly? Do I need to have multiple location and category columns (ie location1, location2, location3, etc...) Or am I complicating this?
Next...
I want a checkbox style search on my site. A user can search by Location and/or by Category. And with checkbox they can choose multiple locations and/or categories. The checkbox values are the IDs of the locations and categories (not the names)
So three ways this can happen.
- Search by locations ONLY
- Search by categories ONLY
- Search by categories within locations
I have two problems.
- I can get scenarios 1 & 2 to work, but only if ONE checkbox is selected.
- I have no idea how to even begin to get scenario 3 to work.
Here is what I have for scenario 1 & 2
$AttorneyLocation = $_POST['AttorneyLocation'];
for ($i="0"; $i<count($AttorneyLocation); $i++) {
if (!is_numeric($AttorneyLocation[$i])) {
$AttorneyLocation[$i]="";
}
if (empty($AttorneyLocation[$i])) {
unset($AttorneyLocation[$i]);
}
}
$AttorneyLocation = implode (" OR ", $AttorneyLocation);
$sqlCommand = "SELECT att_id, att_name, att_logo, att_addy, att_town, att_profile_url FROM attorneys WHERE att_location1='$AttorneyLocation' OR att_location2='$AttorneyLocation' OR att_location3='$AttorneyLocation' OR att_location4='$AttorneyLocation' OR att_location5='$AttorneyLocation'";
Again, this works but only when ONE checkbox is selected, it fails when two or more are selected. Basically it seems to only search the LAST checkbox that has been selected, ignoring the ones before it.
For scenario 3 - Again I'm just not sure where to start, how do I join together the category search within the location search?
If someone can point me in the right direction that would be great, thanks so much! This is my first try creating something like this!
Here is my form code if necessary - all created dynamically
<input type='checkbox' name='AttorneyCategory[]' value='$cat_id'> $category<br />
<input type='checkbox' name='AttorneyLocation[]' value='$loc_id'> $location<br />
$i<count($AttorneyLocation)as the second expression as the count will be evaluated on each iteration. You should assign the value of count to a variable first and use that in your for loop. It is not significant in this case but it is a bad habit to get into.