Aravindan
Aravindan

Reputation: 93

How to show the selected values from database and show in multiple select box in php

i have a array like

$a = array("A", "B", "C");

i have stored the values in database using multiple select.

saved values are "B", "C" in 2 different rows

I need to show the selected value "b" and "D" value in to the same multiple select box.

here the tough concept is i need to display the value of select option from the array as follows

   foreach($dbRows as $dbRow) {
       // here if i display the selected values using if condition the value are selected by the array values repeats like
        a - no selected
        b - selected
        c - no selected

again loops repeats like

       a - no selected
       b - no selected
       c - selected

   }
}

how to display the values in a without repeat?

Upvotes: 0

Views: 602

Answers (1)

Samir Selia
Samir Selia

Reputation: 7065

You can loop the select box array and check if it's value is in DB array using in_array()

$a = array("A", "B", "C");

foreach($a as $v)
{
    $selected = in_array($v, $db_array) ? 'selected' : '';
}

Upvotes: 1

Related Questions