0

I have select tag having multiple attribute in it from which I am saving multiple values in the database using PHP serialize function as serialized array:

Database array which I am inserting:

a:9:{i:0;s:8:"Arkansas";i:1;s:10:"California";i:2;s:8:"Delaware";i:3;s:7:"Montana";i:4;s:14:"North Carolina";i:5;s:12:"North Dakota";i:6;s:8:"Nebraska";i:7;s:13:"New Hampshire";i:8;s:10:"New Jersey";}

Now I want to show these selected values in my SELECT dropdown with SELECTED attribute for the values in my database rest will show as it is non selected.

Example: I have states table and I have selected New York, Miami and save them to database as serialized array. Now, on edit page I want to show New York, Miami as selected while rest states will show as it is non selected.

Here is the code which I am working on:

$result = $mysqli->query("SELECT * FROM `web_states`") or die (mysqli_error());
        $menu   = "<select name='jurisdiction_state[]' class='multiselectState' id='focusedInput' multiple='multiple' size='15' required>";
        $author = unserialize($data);
        foreach( $author as $index=>$key ) {
        $authorRecs = $this->getStateData($key);
        $Recs   = $authorRecs->fetch_object();
        $menu   .= "<option value='".$Recs->state_name."' selected=selected'>".$Recs->state_name."</option>";
        }  
        $menu   .=  "</select>";
        echo $menu;

1 Answer 1

1

I have solved it myself and now the problem is resolved. From my previous code that was not possible.

Here is the serilized array from the database:

a:9:{i:0;s:8:"Arkansas";i:1;s:10:"California";i:2;s:8:"Delaware";i:3;s:7:"Montana";i:4;s:14:"North Carolina";i:5;s:12:"North Dakota";i:6;s:8:"Nebraska";i:7;s:13:"New Hampshire";i:8;s:10:"New Jersey";}

I simply unserialize it using:

$data = unserialize(a:9:{i:0;s:8:"Arkansas";i:1;s:10:"California";i:2;s:8:"Delaware";i:3;s:7:"Montana";i:4;s:14:"North Carolina";i:5;s:12:"North Dakota";i:6;s:8:"Nebraska";i:7;s:13:"New Hampshire";i:8;s:10:"New Jersey";})

and get the 1st index of that array which returns State Names in the form of array.

So finally I have used in_array() to check the selected and non selected and got the final result.

Here is the working code.

$result = $mysqli->query("SELECT state_name FROM `web_states`") or die (mysqli_error());

$menu   = "<select name='jurisdiction_state[]' class='multiselectState' id='focusedInput' multiple='multiple' size='15' required>";

$author[1] = unserialize($data);

while($rcData = $result->fetch_array()){

if(in_array($rcData['state_name'], $author[1])){

$menu .=  "<option value='".$rcData['state_name']."' selected='selected'>".$rcData['state_name']."</option>";

}else{

$menu .=  "<option value='".$rcData['state_name']."'>".$rcData['state_name']."</option>";

    }
}
$menu .=  "</select>";
echo $menu;

Hope it helps someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.