Two foreach in a select

Asked

Viewed 320 times

0

Good evening. How do I use foreach suns to check if the names are equal and so leave them with selected.

Follows the returns of the variables:

$risco_e =

    Array
    (
        [0] => Postura Inadequada
        [1] => Postura Inadequada por tempo Indetermidado
    )




$risco_ergonomico = 
    Array
    (
        [0] => stdClass Object
            (
                [idRiscoErgonomico] =>; 5
                [riscoErgonomico_Nome] =>; Esforço Fisico
            )

        [1] => stdClass Object
            (
                [idRiscoErgonomico] =>; 1
                [riscoErgonomico_Nome] =>; Não Exposto
            )

        [2] => stdClass Object
            (
                [idRiscoErgonomico] =>; 2
                [riscoErgonomico_Nome] =>; Odontologos
            )

        [3] => stdClass Object
            (
                [idRiscoErgonomico] =>; 3
                [riscoErgonomico_Nome] =>; Posições Encomodas
            )

        [4] => stdClass Object
            (
                [idRiscoErgonomico] =>; 6
                [riscoErgonomico_Nome] =>; Postura Inadequada
            )

        [5] => stdClass Object
            (
                [idRiscoErgonomico] =>; 4
                [riscoErgonomico_Nome] =>; Postura Inadequada por tempo Indetermidado
            )

    )

I tried so and kind that worked, the same values are selected, but all are duplicated, as shown in the following photo:

<select>
  <?php

    str = trim($result->setor_RiscoErgonomico, ',');
    $risco_e = explode(',', $str);

    foreach ($risco_ergonomico as $riscoErgonomico)
    { 
       foreach ($risco_e as $re)
       {  ?>

          <option <?=$re == $riscoErgonomico->riscoErgonomico_Nome ? "selected" : ""?>><?=$riscoErgonomico->riscoErgonomico_Nome?></option>

       <?php }                        
     } 

  ?>
</select>

Foto de anexo de teste do código

  • I don’t understand your doubt

  • Normally it was only to appear one name of each risk, but when performing the two foreach are being duplicated the values.

2 answers

1


I changed your foreach a little bit:

foreach ($risco_ergonomico as $riscoErgonomico)
{ 
    $descricao = $riscoErgonomico->riscoErgonomico_Nome;
    $selectd = "";
    foreach ($risco_e as $re) {
        if($re == $riscoErgonomico->riscoErgonomico_Nome){
            $selectd = "selected";
        }
   }
   echo("<option $selectd>$descricao</option>");
 } 

So the values were not duplicated. The idea is this, now you improve the code.

  • Thanks brother. All right.

0

You can try using the method in_array, to check if the data already exists inside the array and thereby deny that the data enters:

foreach ($risco_ergonomico as $riscoErgonomico)
{ 
   foreach ($risco_e as $re)
   {  ?>

      if(!in_array($riscoErgonomico, $re)){
        <option <?=$re == $riscoErgonomico->riscoErgonomico_Nome ? "selected" : ""?>><?=$riscoErgonomico->riscoErgonomico_Nome?></option>
      }

   <?php }                        
 } 

You would also have the option to take one array and play inside the other using "array_unique" if all keys are equal:

$risco_ergonomico = array_unique($risco_e);
  • None of the options gave certain friends. ( The worst that this system need to deliver this week.

  • Inside the in_array, in the first parameter tries to pass the name, it did not work because they are not identical arrays: ! in_array($riscoErgonomico->riscoErgonomico_Nome, $re)

  • Another thing, I believe you don’t need to do two arrays, just one running the $risco_e and putting it inside $risco_ergonomico.

  • Could you explain to me better, Paul?

  • Can someone help me?

Browser other questions tagged

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