【发布时间】:2015-06-26 14:56:03
【问题描述】:
问题:绘制复选框并将 query1 中的 ID 分配给 elementId,并将值分配给 query2 中的复选框。但问题是查询 2 与 array1 相比并不总是返回完整的 9 个值。在比较 array1 值之后,我想用零替换查询 2 中的缺失值。
我的方法:
获取两个查询的结果集和
query 1 resultset = $tresult;
query 1 resultset = $assignedTiles;
$a1 = array();
while ($row = mysql_fetch_row($tresult)){
$a1[] = $row;
}
$a2 = array();
if (mysql_num_rows($assignedTiles)==0){
//echo " inside if row null";
$a2 = array ('0' => array ('0'),'1' => array ('0'),'2' => array
('0'),'3' => array ('0'),'4' => array ('0'),'5' => array ('0'),
'6' => array ('0'),'7' => array ('0'),'8' => array ('0'));
}else{
while($row = mysql_fetch_row($assignedTiles)){
$a2[] = $row;
}
}
foreach($a1 as $aV){
$aTmp1[] = $aV['0']; //setting array key
}
foreach($a2 as $aV){
$aTmp2[] = $aV['0'];
}
$resultArr = array_diff($aTmp1,$aTmp2);
// getting the difference in both arrays
if($resultArr !=NULL){
foreach($resultArr as $v){
$v = str_replace($resultArr, "0", $aTmp1);
}
}else { echo "did not match"; }
$countVal = count($v);
$i=0;
$i<$countVal;
$i++;
foreach ($v as $tileId => $value){
echo "<td align=center >
<input type='checkbox' id='checkBox$tileId ' value='$value' >
</td>";
}
I'm pretty sure there is a better way to do this. Any ideas or suggestion
would be really appreciated.
【问题讨论】:
-
您正在使用关系数据库。你为什么要在 php 中做交集代码,而你可以让 mysql 为你做这些? RDBMes 就是为这种事情而设计的。
-
SELECT IFNULL (searchwords,'null to string') as searchwords FROM
table_article; @Marc B 是正确的。 -
我理解你的意思,但这是我能想到的。正如你所说,有 100 种方法可以解决上述问题。
标签: php arrays comparison array-difference