mallen Posted January 31, 2012 Share Posted January 31, 2012 I have a form that emails the content to a recipient. Now I want to direct the email to 6 different recipients based on the county they select. I got it to work with just two counties (cases). But my state has over 60 counties and I will be dividing by about 10 counties each. Is there a way to list 10 counties for each case? This is what I have. switch ($to) { case 'County1': $to = "[email protected]"; break; case 'County2': $to = "Bob@mysite"; break; default: $to = "[email protected]"; } Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/ Share on other sites More sharing options...
codebyren Posted January 31, 2012 Share Posted January 31, 2012 I believe you can stack cases like so: <?php $x = 5; switch ($x) { case 5: case 12: echo "x is 5 or 12"; break; default: echo "x is neither 5 nor 12"; break; } exit; ?> You could also stash the counties in different arrays and then set $to based on a series of if statements using the in_array function: http://nz.php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/#findComment-1313066 Share on other sites More sharing options...
digibucc Posted January 31, 2012 Share Posted January 31, 2012 there is no limit to case. here's a snip from one of my scripts: <?php switch($stl_type){ default:$type = 'other-trucks';break; case 'car carrier truck':$type = 'auto-carrier';break; case 'van trucks / box trucks':$type = 'box-vans';break; case 'bucket truck / boom truck':$type = 'bucket-truck';break; case 'dump truck': case 'dump bodies only': case 'dump chassis truck': case 'dump/transfer': case 'flatbed-dump truck':$type = 'dump-trucks';break; case 'tow trucks': case 'roll-back tow truck': case 'wrecker tow truck': case 'tow truck':$type = 'tow-trucks ';break; } ?> notice: <?php case 'dump truck': case 'dump bodies only': case 'dump chassis truck': case 'dump/transfer': case 'flatbed-dump truck': $type = 'dump-trucks'; break;?> so you can set multiple matches for an action. my statement is 3x that long, and i don't believe there is a functional limit. maybe there error is elsewhere, or syntax? you could also put them in arrays: <?php $region1 = array('county1', 'county2', 'county3', 'etc'; $region2 = array('county4', 'county5', 'county6', 'etc'; if (in_array($needle, $region1) { do_something(); // found your county in region 1 } else if (in_array($needle, $region2) { do_something(); // found your county in region 2 }?> if you do more, you may consider making them array keys and using array_key_exists, as that's faster than searching array values. but for 10 counties each region this works fine. Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/#findComment-1313067 Share on other sites More sharing options...
mallen Posted February 1, 2012 Author Share Posted February 1, 2012 Thanks for your replies. This is what I came up with before I saw the replies. Is there any benefit to using an array over the way I did it? (more text fields above this) $county = $_POST['county']; $to = $county; switch ($to) { case ($to =='County1' || $to == 'County2' || $to == 'County3'): $to = "[email protected]"; break; case ($to == 'County4' || $to == 'County5' || $to == 'County6'): $to = "[email protected]"; break; default: $to = "[email protected]"; } Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/#findComment-1313185 Share on other sites More sharing options...
Drummin Posted February 1, 2012 Share Posted February 1, 2012 In my opinion an array would work well as you can use a foreach loop to send out the emails. Plus a lot less coding adding all those case conditions. Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/#findComment-1313188 Share on other sites More sharing options...
digibucc Posted February 1, 2012 Share Posted February 1, 2012 agreed with drummin - switch assuredly has it's uses but for this i'd use an array. also, you can set your var as such: <?php $to = $county = $_POST['county']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/#findComment-1313300 Share on other sites More sharing options...
PFMaBiSmAd Posted February 1, 2012 Share Posted February 1, 2012 Your relationship between counties and email address should be stored in a data structure (database, array) so that all you need to do to change, add, or remove any of the relationships is to change the data that is stored in the data structure. The logic in your application should remain the same no matter how much data the is or what the actual relationship is that the data defines. Quote Link to comment https://forums.phpfreaks.com/topic/256135-sending-form-to-recipient-based-on-location/#findComment-1313303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.