Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 7-Feb-18 11:01am

1 solution

Okay, I figured it out. This does the trick:

strpos($saved_counties, $one_county['County'])!==false
 
Share this answer
 
Comments
Dave Kreskowiak 7-Feb-18 17:26pm    
What are you doing with this list of Michigan county names?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900