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条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?