您好我有一个接收数组的函数,并且对于数组中的每个元素,它运行一个选择随机结果ID的查询,但是我想避免重复,我如何检查结果id是否重复并运行再次询问?所有在循环中而不是滑动元素?这是我的尝试 (注意:$ array_result是7个元素),也选择distinct不工作因为我必须单独为每个数组元素运行查询
$queryn = "select taxonomic_units.tsn, hierarchy.hierarchy_string, hierarchy.TSN, taxonomic_units.rank_id from hierarchy left join taxonomic_units on hierarchy.TSN = taxonomic_units.tsn where taxonomic_units.rank_id = 220 and hierarchy.hierarchy_string LIKE '%$array_result[0]%'order by rand() limit 1";
$resultn = $dbn -> Execute($queryn);
$rown=$resultn->FetchRow();
$newspecies = $rown['tsn'];
$result_array[] = $newspecies;
for($i=1; $i<count($array_result);$i++){
$previous = implode(',', $result_array);
$queryn = "select taxonomic_units.tsn,
hierarchy.hierarchy_string,
hierarchy.TSN,
taxonomic_units.rank_id
from hierarchy
left join taxonomic_units
on hierarchy.TSN = taxonomic_units.tsn
where taxonomic_units.rank_id = 220
and hierarchy.hierarchy_string LIKE '%$array_result[$i]%'
and taxonomic_units.tsn not in ('$previous')
order by rand()
limit 1";
$resultn = $dbn -> Execute($queryn);
$rown=$resultn->FetchRow();
$newspecies = $rown['tsn'];
$result_array[] = $newspecies;
}
答案 0 :(得分:0)
您可以在存储IS之前搜索重复项。使用in_array
简化。
function getrandomspecies($array_result){
...
$i=0; //If your $array_result begin with 0
while($i<count($array_result))
{
...
$newspecies = $rown['tsn'];
if(in_array($newspecies, $result_array)))
continue;
else
{
$result_array[] = $newspecies;
$i++;
}
}
}
答案 1 :(得分:0)
你的$ array_result结构是什么?
array(''=&gt;'');
或
array(array(''=&gt;''));
如果您的数组限制为第1,那么您可以使用array_unique检查重复的ID。
如果你的数组结构是第二个,你可能需要在运行查询之前做一些过滤功能..
答案 2 :(得分:0)
function getrandomspecies($array_result){
$dbn = adodbConnect(); //connect to database
$result_array = array(); //make empty array for result array
$i=0;
a: foreach($array_result as $id){ // this loops 7 times
for($j=0;$j<=$i;$j++)
{
if($array_result[$j]==$id)
{
goto a; //will go to foreach loop if there is a previous occurence of the element
}
}
//complicated query that basically chooses a random id based on a likeness of a string that has other ids
$queryn = "select taxonomic_units.tsn, hierarchy.hierarchy_string, hierarchy.TSN, taxonomic_units.rank_id from hierarchy left join taxonomic_units on hierarchy.TSN = taxonomic_units.tsn where taxonomic_units.rank_id = 220 and hierarchy.hierarchy_string LIKE '%$id%'order by rand() limit 1";
$resultn = $dbn -> Execute($queryn);
$rown=$resultn->FetchRow();
$newspecies = $rown['tsn'];
$result_array[] = $newspecies; //this is the result array and the one result putting in
}
}
答案 3 :(得分:0)
你可以这样做:
$previous = implode(',', $result_array);
$queryn = "select taxonomic_units.tsn,
hierarchy.hierarchy_string,
hierarchy.TSN,
taxonomic_units.rank_id
from hierarchy
left join taxonomic_units
on hierarchy.TSN = taxonomic_units.tsn
where taxonomic_units.rank_id = 220
and hierarchy.hierarchy_string LIKE '%$id%'order
and taxonomic_units.tsn not in (".$previous.")
order by rand()
limit 1";
请注意最终的and
,不包括您已检索过的tsn
。它可能需要稍微调整(检查$result_array
是否为空等),但这是基本的想法。