Hi, I have a form that searches the practice areas and states that law firms do business in. I need the user to have the ability to select multiple practice areas and multiple states (if they want to) to conduct their search. After submitting the form, I would like to combine the practice area selections into one variable, sorted alphabetically...and combine the selected states into another variable, sorted alphabetically. My form looks something like this:
<form name="directory" action="" method="POST">
<select name="PRACTICE[0]">
<option value="">All Practice Areas</option>
<option value="Appellate">Appellate</option>
<option value="Bankruptcy">Bankruptcy</option>
<option value="Commercial">Commercial</option>
<option value="Fraud">Fraud</option>
</select>
<select name="PRACTICE[1]">
<option value="">Additional Practice Area</option>
<option value="Appellate">Appellate</option>
<option value="Bankruptcy">Bankruptcy</option>
<option value="Commercial">Commercial</option>
<option value="Fraud">Fraud</option>
</select>
<select name="STATE[0]">
<option value="">All States</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="MA">MA</option>
<option value="RI">RI</option>
</select>
<select name="STATE[1]">
<option value="">Additional State</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="MA">MA</option>
<option value="RI">RI</option>
</select>
</form>
I am very new to PHP and any help would be greatly appreciated. Thank you very much!
Combine and Sort Form Values
Moderator: General Moderators
Re: Combine and Sort Form Values
Instead of using <option> would it be easier to have checkboxes?
Then they can select as many as they like.
Did you want a delimited string or an array returned after submission?
Then they can select as many as they like.
Did you want a delimited string or an array returned after submission?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Combine and Sort Form Values
Try this function
Hth
Code: Select all
<?php
/**
* group elements of similar name
*
* this method groups select elements with a similar name
*
* @param string $name Name of select element
* @param int $number_of_elements Number of elements that share a names
*
*/
function groupTogetherByName($name, $number_of_elements) {
$group_name = trim($name);
for ($i = 0; $i < $number_of_elements; $i++) {
$value = $_POST[$group_name][$i];
$group[] = $value;
}
return $group;
}
// this part is how to use, and how you can check the results
$array1 = groupTogetherByName('PRACTICE', 2);
$array2 = groupTogetherByName('STATE', 2);
echo '<pre>';
print_r($array1);
print_r($array2);
echo '</pre>';
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: Combine and Sort Form Values
Okay, you were right, checkboxes will work better for what I want to do. You can see the page I have been working on here: http://www.namwolf.org/extended-law-firm-directory
After I got the two arrays from the search form I imploded them as comma separated strings and searched that way, however that won't always work when searching for multiple practice areas or states because the results don't account for a law firm that practices in AL, AK, and AZ when a user only selects AL and AZ. (hopefully that makes sense). In other words, it searches for "AL, AZ" when that is what was chosen but a firm with "AL, AK, AZ" won't appear in the search results because it doesn't match the string.
I don't understand how to setup the search properly. Any help would be greatly appreciated. I'm working from a WordPress database with this site, if that helps. Let me know if you need any more info. Here is the code I have following my form (LFPA is law firm practice areas and LFST is law firm states):
<?php
$array1 = $_POST['LFPA'];
$array2 = $_POST['LFST'];
$LFPA = implode(', ', $array1);
$LFST = implode(', ', $array2);
echo '<strong>Your search for ';
if($LFPA != '') {
echo $LFPA;
} else {
echo 'all practice areas';
}
echo ' in ';
if($LFST != '') {
echo $LFST;
} else {
echo 'all states';
}
echo ' returned the following results:</strong>';
global $wpdb, $table_prefix; // set global WP vars needed for script
$sql = "SELECT ID FROM $wpdb->users AS users
LEFT JOIN $wpdb->usermeta AS practice_areas ON users.ID=practice_areas.user_ID AND practice_areas.meta_key='practice_areas'
LEFT JOIN $wpdb->usermeta AS branch_office_locations ON users.ID=branch_office_locations.user_id AND branch_office_locations.meta_key='branch_office_locations' WHERE 1=1";
if($LFPA != '') $sql .= " AND practice_areas.meta_value REGEXP '$LFPA'";
if($LFST != '') $sql .= " AND branch_office_locations.meta_value REGEXP '$LFST'";
$sql .= " ORDER by display_name";
?>
After I got the two arrays from the search form I imploded them as comma separated strings and searched that way, however that won't always work when searching for multiple practice areas or states because the results don't account for a law firm that practices in AL, AK, and AZ when a user only selects AL and AZ. (hopefully that makes sense). In other words, it searches for "AL, AZ" when that is what was chosen but a firm with "AL, AK, AZ" won't appear in the search results because it doesn't match the string.
I don't understand how to setup the search properly. Any help would be greatly appreciated. I'm working from a WordPress database with this site, if that helps. Let me know if you need any more info. Here is the code I have following my form (LFPA is law firm practice areas and LFST is law firm states):
<?php
$array1 = $_POST['LFPA'];
$array2 = $_POST['LFST'];
$LFPA = implode(', ', $array1);
$LFST = implode(', ', $array2);
echo '<strong>Your search for ';
if($LFPA != '') {
echo $LFPA;
} else {
echo 'all practice areas';
}
echo ' in ';
if($LFST != '') {
echo $LFST;
} else {
echo 'all states';
}
echo ' returned the following results:</strong>';
global $wpdb, $table_prefix; // set global WP vars needed for script
$sql = "SELECT ID FROM $wpdb->users AS users
LEFT JOIN $wpdb->usermeta AS practice_areas ON users.ID=practice_areas.user_ID AND practice_areas.meta_key='practice_areas'
LEFT JOIN $wpdb->usermeta AS branch_office_locations ON users.ID=branch_office_locations.user_id AND branch_office_locations.meta_key='branch_office_locations' WHERE 1=1";
if($LFPA != '') $sql .= " AND practice_areas.meta_value REGEXP '$LFPA'";
if($LFST != '') $sql .= " AND branch_office_locations.meta_value REGEXP '$LFST'";
$sql .= " ORDER by display_name";
?>