0

so i have 2 lists i need to compare

$list = array("Apple", "Orange", "Lemon", "Candy");

and

$db = $r_bio['other'];
$db2 = explode(":", $db);

i want to compare the two and if i find the same i want to echo a checked checkbox if not an unchecked checkbox

if($db2 == $list)
{ echo "<input type=checkbox checked value=$list />$list"; } else { echo "<input type=checkbox value=$list /> $list"; }

the $db2 can contain 1, 2, ..., or all of the items in the $list

3
Contributors
5
Replies
20
Views
3 Years
Discussion Span
Last Post by foxwizzy
0

You can use a loop with in_array():

foreach($db2 as $key)
{
    echo in_array($key, $list) ? "<input type='checkbox' checked value='$list' />$list" : "<input type='checkbox' value='$list' /> $list";
}

More information: http://php.net/in_array

0

That works....50%
it only echo's out the values that exits in $db2(the checked ones)
i need to print the rest too(the unchecked ones)

Edited by foxwizzy

1

Then reverse it:

foreach($list as $key)
{
    echo in_array($key, $db2) ? "<input type='checkbox' checked value='$key' />$key" : "<input type='checkbox' value='$key' /> $key";
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.