Check if there is duplicate value in while

Asked

Viewed 34 times

0

I’m half a day trying to do this.

I have this loop

if(@mysqli_num_rows($sql_es_1_aux) == 1){
   echo "";
}else{
   $sql_es_1 = mysqli_query($config, "SELECT peso FROM tb_sub_agrupamento 
                                      WHERE id_ctr IN ($id_ctr_ok) AND 
                                       peso <> ''") or die(mysqli_error($config));


  if(@mysqli_num_rows($sql_es_1) <= '0'){
    echo "";
  }else{

       while($r_sql_es_1 = mysqli_fetch_array($sql_es_1)){

           $peso_sel_es1 = $r_sql_es_1[0];                                                  
        }
    }
}

$peso_sel_es1 may be repeated.

Is there any way to check if it is repeated outside the while?

If I turn it into array (putting []) help?

I need to determine that if it’s repeated, don’t show the weight.

  • You want to display the repeated value only once right?

  • Not exactly. I need to check only if it exists or not repeated. After checking, it will be a simple if. If it has repeated it does not show the block.

1 answer

1


You can solve in your query by grouping the weights. And counting how many of the same exist. Ai in PHP you compare if the repetition is greater than 1.

SELECT 
    peso, 
    COUNT(peso) AS repeticoes 
FROM 
    tb_sub_agrupamento 
WHERE 
    id_ctr IN ($id_ctr_ok) 
    AND 
    peso <> '' 
GROUP BY 
    peso

Or if you do not want to show the repeated weights, only the ones that are unique. The query thus also resolves. Not having need of business rule at source.

SELECT 
    peso, 
    COUNT(peso) AS repeticoes 
FROM 
    tb_sub_agrupamento 
WHERE 
    id_ctr IN ($id_ctr_ok) 
    AND 
    peso <> '' 
GROUP BY 
    peso
HAVING
    repeticoes = 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.