0

嗨,我有一个接收数组的函数,对于数组中的每个元素,它运行一个选择随机结果 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;
}
4

4 回答 4

0

您可以在存储该 ID 之前搜索重复项。使用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++;
       }
   }
}
于 2013-08-07T04:02:27.823 回答
0

你的 $array_result 结构是什么?

数组(''=>'');

或者

数组(数组(''=>''));

如果您的数组严格是第一个,那么您可以使用array_unique来检查重复的 id。

如果您的数组结构是第二个,您可能需要在运行查询之前执行一些过滤功能。

于 2013-08-07T04:02:45.487 回答
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
 }    

}

于 2013-08-07T04:51:08.077 回答
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为空等),但这是基本思想。

于 2013-08-07T04:57:16.117 回答