dpdhf02040 2015-09-18 16:17
浏览 44
已采纳

如何使用in_array创建EXACT MATCH

This Question has been re-worked to better explain:

I have a form, with a field called assign to. The field is a SELECT with MULTIPLE enabled. That field is being hooked by Jquery's autocomplete. The OPTIONS consist of an employee list, as the form is a "follow-up" ticket generator and a ticket can be assigned to multiple employees for follow-up.

The OPTIONS list is being generated by a PHP query from database getting all records from table EMPLOYEES, The OPTIONS list is made by a ECHO statement inside a WHILE LOOP. each option consists of this <option value='$rowx[employeeid],$rowx[employeename]' $keyx>$rowx[group] - $rowx[employeename]</option>

This form when in a BLANK state works just fine, but the same form is used and PRE-POPULATED with data when a user is trying to EDIT a follow-up ticket as opposed to creating a new one.

In order to PER-POPULATE the MULTI-SELECT: the database has a field called assignto consisting of employee ID numbers (comma separated) that the follow-up being edited has been previously assigned to: Thus by example [assignto]="2345,234567,34657,32432" (Thats 4 different employees it has been tasked too)

The code below is whats generating the options list, the INTENT was that if $rowx['employeeid'] matches any value that was already in the assignto row from the database then THAT OPTION would have the SELECTED attribute added to it. Thus giving me a per-populated multi-select. The Problem: When editing a follow-up where employee "234567" was assigned, the SELECTED is being added to 2 option fields. John doe - Employee #2345 & Jim Doe - Employee #234567 because PHP in_array isn't looking for explicitly "23456", but instead is looking for <---- "23456" ---->

<select name="assignto[]" type="text" class="mselects" multiple="multiple" style="width:460px; overflow:hidden;">
<?php
$equery = "SELECT `employeeid`,`employeename`,`group` FROM `employees` WHERE `status` = 'A' ";
$result = mysqli_query($con,$equery);
$litmus = explode(",",$row['assignto']);
while ($rowx = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$keyx = array_search($rowx['employeeid'], $litmus);
if ($keyx >= 0) {$keyx = "selected";}
echo "<option value='$rowx[employeeid],$rowx[employeename]' $keyx>$rowx[group] - $rowx[employeename]</option>";
unset($keyx);
}
 ?>
</select>
  • 写回答

3条回答 默认 最新

  • duanche4578 2015-09-18 17:29
    关注

    Currently your check if ($keyx >= 0) will always eval to true.

    if ($keyx !== false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料