This is making my eyes go nuts right now. It should work but it's not. I'm storing the values (county names) the user chose from a multiselect select in a comma delimited string. Then when the form is submitted and rejected I'm using that string to "select" the ones that were previously selected.
What I have tried:
Here's my test code:
<?php
echo 'Previous selection: '.$saved_counties;
echo '<br /><br />List of all counties:';
foreach ($counties as $one_county):
echo '<br />'.$one_county['County'];
if (strpos($saved_counties, $one_county['County'])) {
echo ' (**** IN PREVOUS SELECTION ***)';
}
endforeach;
?>
When I run the code, this is what I get:
Previous selection: Baraga,Barry,Bay
List of all counties:
Alcona
Alger
Allegan
Alpena
Antrim
Arenac
Baraga
Barry (**** IN PREVOUS SELECTION ***)
Bay (**** IN PREVOUS SELECTION ***)
Benzie
Berrien
Branch
Calhoun
Cass
Charlevoix
Cheboygan
Chippewa
Clare
Clinton
Crawford
Delta
Dickinson
Eaton
Emmet
Why is it skipping "Baraga"? It doesn't matter which group I select. It always skips the first one in the "foreach" code. I tried:
strpos($saved_counties, $one_county['County']) >= 0
That rendered the same results.
I also tried:
strpos($saved_counties, $one_county['County']) > 0
That ended up reselecting ALL of them.
$counties is an array that stores ALL 83 michigan counties. $saved_counties is just a comma delimited string that stores the user's previous selection.