如何使用in_array创建EXACT MATCH

时间:2015-09-18 16:17:51

标签: php

此问题已经过重新设计,以便更好地解释:

我有一个表单,有一个名为assign的字段。该字段是启用了MULTIPLE的SELECT。该领域被Jquery的自动完成所吸引。 OPTIONS由员工名单组成,因为表格是"后续"可以将票证生成器和票证分配给多个员工进行跟进。

OPTIONS列表由来自数据库的PHP查询生成,从表EMPLOYEES获取所有记录,OPTIONS列表由WHILE LOOP内的ECHO语句生成。每个选项都包含此<option value='$rowx[employeeid],$rowx[employeename]' $keyx>$rowx[group] - $rowx[employeename]</option>

当处于BLANK状态时,此表单工作得很好,但是当用户尝试编辑后续票证而不是创建新票据时,使用相同的表格和PRE-POPULATED数据。

为了对MULTI-SELECT进行PER-POPULATE:数据库有一个名为assignto的字段,该字段由员工ID号(逗号分隔)组成,后面正在编辑的后续内容先前已分配给: [assignto] =&#34; 2345,234567,34657,32432&#34; (这也是4个不同的员工,它的任务也是如此)

下面的代码是生成选项列表的原因,INTENT是如果$rowx['employeeid']匹配数据库中assignto行中已有的任何值,那么THAT OPTION将添加SELECTED属性它。从而给我一个人口稠密的多选。问题:当编辑后续员工&#34; 234567&#34;已分配,SELECTED将添加到2个选项字段。 John doe - 员工#2345&amp; Jim Doe - 员工#234567,因为PHP in_array并没有明确地寻找&#34; 23456&#34;,而是寻找&lt; ----&#34; 23456&#34; ----&GT;

<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 个答案:

答案 0 :(得分:0)

foreach ($value in $litmus) {
    if ($value==$rowx['employeeid']) {

    }
}

答案 1 :(得分:0)

正如Jason告诉你的那样,当数组包含你想要的任何元素的值时,in_array返回一个布尔值。

您可能需要查看array_search()。

答案 2 :(得分:0)

目前检查if($ keyx&gt; = 0)是否总是eval为true。

org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.isValid(DelegatingConnection.java:913)

org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConnection.java:226)

org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:302)

org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2208)

org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2191)

org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1945)

org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)

com.networkfleet.sre.common.db.Resource.getDBConnectionDEV(Resource.java:75)

com.networkfleet.sre.app.checkreport.CheckReportSrv.doGet(CheckReportSrv.java:63)

javax.servlet.http.HttpServlet.service(HttpServlet.java:622)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)

org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)

org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)

org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)

org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)

org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)

org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)

org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)

org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2463)

org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2452)

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

java.lang.Thread.run(Thread.java:745)