0
votes

I have the below code to generate a multiple select box with values from a database. The page in question is a form for modifying previously entered information. The value of “$Interviewers0” is retrieve before the box is generated. If the value of “$Interviewers0” is “John Doe” it selects that name fine. But if I have a value of “John Doe, Jane Doe” none of the names are selected. I know it’s because of the imploded value but I haven’t been able to figure it out. Thanks in advance!

{
$box1 = array();
 $result = "SELECT FullName FROM UserInformation";
  $rs=odbc_exec($conn,$result);
while($row = odbc_fetch_array($rs)) { $box1[] = $row; }
}
 $Interviewers = '<select name="Interviewers[]" multiple="multiple" size="5">';
  $Interviewers .= '<option>---< Select Interviewers >---</option>';
if (!empty($box1)) {foreach ($box1 as $k => $v)
 {if(!empty($Interviewers0)){$Interviewers .= '<option value="'.$v['FullName'].'"'. (!strcmp($v['FullName'],$Interviewers0)?' selected':'').'>'.$v['FullName'].'</option>';}
  else
 {$Interviewers .= '<option value="'.$v['FullName'].'">'.$v['FullName'].'</option>';}
}
}
$Interviewers .= '</select>';
echo $Interviewers;
2
I don't see any implode() in your code...Justin Wood
Learn to properly indent your code please and don't be afraid of using whitespace. Nobody is going to think you are a racist for doing so.PeeHaa

2 Answers

1
votes

What you are looking for can be done with strpos:

So, use:

(strpos($v['FullName'], $Interviewers0) !== false) ? ' selected' : ''

instead of:

!strcmp($v['FullName'],$Interviewers0)?' selected':''

With (strpos($v['FullName'], $Interviewers0) !== false) ? ' selected' : '', if $Interviewers0 is found in $v['FullName'] then the option is selected.

0
votes

I replaced

(!strcmp($v['FullName'],$Interviewers0)?' selected':'')

with

((stripos($Interviewers0, $v['FullName']) !== false) ?' selected':'')

and that did it.