foreach inside the foreach without repeating the data

Asked

Viewed 469 times

0

I have a list of Employees and each employee can have many Cost Centers.

And when I bring the bank employee information I need to list the cost centers and list the cost centers where the employee is registered by marking the select as Selected.

$intCentroCustoId = $this->modelo->arrComboCentroCustoFuncionario; // traz o array da listagem de centros de custo que o funcionario está cadastrado
    $arrComboBar = $this->modelo->arrComboBar; // traz todos os centros de custos
    /*echo '<pre>';
    var_dump($intCentroCustoId);
    echo '</pre>';*/

    echo "<select data-minlength-error='Selecione pelo menos um'
                                                data-placeholder='Digite ou selecione' data-minlength='1'
                                                multiple='multiple' name='arrCentroCusto[]'
                                                id='arrCentroCusto'
                                                class=\"js-basic-multiple form-control\" required>"; // monta a select multiplo
    if(isset($arrComboBar) && !empty($arrComboBar)){ // verifica se a combo não esta vazia
        echo "<option></option>";//primeiro option vazio
        foreach($arrComboBar as $arrLinha){ //listagem completa dos centros de custos
            foreach ($intCentroCustoId as $CC)
                    if($CC['id_centro_custo'] == $arrLinha['idcusto']){
                        echo "<option value='".$CC['id_centro_custo']."' selected>";
                        echo utf8_decode($arrLinha['departamento']);
                        echo "</option>";
                    }

                    else{
                        echo "<option value='".$arrLinha['idcusto']."'>";
                        echo utf8_decode($arrLinha['departamento']);
                        echo "</option>";
                    }


        }

    }
    echo "</select>";

My problem is that the listing is repeated, depending on how many cost centers the employee is registered.

inserir a descrição da imagem aqui

How do I list all cost center with the 3 selected and without repeating?

  • Hello Jonathan all good? This data you’re bringing from a database is that it? no select that brings the database data you are using ' SELECT DISTINCT column FROM table_name; 'so you will not have any repeat data coming from the database. But in case I’m wrong, I may not understand your question well.

  • My question is, how not to repeat the values that are in Select Option. There are 3 tables, the one that lists the employees, the one that lists the centres_cost and the one that lists centro_cost_employees.

1 answer

0


Dude, the problem is that your option echo is inside the 2 foreachs, then it will repeat, I took the echo from inside one of the foreach and gave a summary in your code:

if(isset($arrComboBar) && !empty($arrComboBar)){ // verifica se a combo não esta vazia

    echo "<option></option>";//primeiro option vazio

    foreach($arrComboBar as $arrLinha){ //listagem completa dos centros de custos
      foreach ($intCentroCustoId as $CC){

        if($CC['id_centro_custo'] == $arrLinha['idcusto']){
          $selected = ' selected';
          break;
        }else{
          $selected = '';
        }

      }

      echo "<option value='".$arrLinha['idcusto']."'".$selected.">";
      echo utf8_decode($arrLinha['departamento']);
      echo "</option>";

    }
}

Browser other questions tagged

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