Foreach inside foreach is duplicating PHP values? [closed]

1

I'm having a problem with looping , I'm working with 3 Bank tables, it would be Anuncio , Opcoes_Anuncio ( interleave) and Opcoes , I'm trying to get data from options together with the selected options, but it happens that you are giving multiple looping .

MYSQL

My query is like this

  

SELECT * FROM opcoes op INNER JOIN opcoes_anuncio oa ON    oa . opcoes_id_opcao op . id_opcao INNER JOIN anuncio an ON    oa . anuncio_id_anuncio = an . id_anuncio WHERE    an . anuncio_id_anuncio = '17' ORDER BY op . id_opcao ASC

PHP      

<?php foreach ($opcoes as $val): ?>
    <div class="form-group form-check">
      <input type="checkbox" class="form-check-input" name="opcoes_id_opcao[]"
              value="<?= $row->id_opcao; ?>"
              id="<?= $row->slug_opcao; ?>" 
            <?php if($row->id_opcao == $val->opcoes_id_opcao) { echo "checked"; }?> >
        <label class="form-check-label" for="<?= $row->slug_opcao; ?>">
           <?= $row->titulo_opcao; ?>
        </label>
        </div>
<?php endforeach; ?>
<?php endforeach; ?>
  

    
asked by anonymous 15.05.2018 / 23:31

1 answer

1

Hello, it's hard to know why this occurs without seeing the structure of the tables, but a group by opcoes_anuncio.anuncio_id_anuncio can solve the problem.

Also, it looks like your code is not right, it has 2 endforeach , and you iterate with the variable $val , but below it uses $row . It looks like the code is wrong or incomplete.

    
16.05.2018 / 15:22