Select no option

-1

I have a accounts payable screen, and when I edit the data, the option field is showing the name of the supplier ( Liquigas ), so far so good. But when I click on the list, the vendor's name is appearing twice. What can I do wrong?

                <div class="form-group col-md-10">
                <label>Fornecedor</label>

                <select class="form-control form-control-sm" name="cod_for">
                    <?php

                    $sql_nome_for = "SELECT nome FROM cad_for WHERE id = '$cod_fornecedor'";
                    $sql_nome_for = $pdo->query($sql_nome_for);
                    $dado_for = $sql_nome_for->fetch();
                    ?>

                    <option><?php echo utf8_encode($dado_for['nome']); ?></option>
                    <?php   
                    $sql_for = "SELECT id, nome FROM cad_for WHERE del <> '1' ORDER BY nome";

                    $sql_for = $pdo->query($sql_for);

                    If($sql_for->rowCount()>0){

                        foreach($sql_for as $fornecedor):
                            ?>
                        <option value="<?php echo $dado['id_cad_for']; ?>" ><?php echo utf8_encode($fornecedor['nome']); ?></option>
                        <?php 
                        endforeach;
                    }
                    ?>
                </select>
            </div>

    
asked by anonymous 04.09.2018 / 04:35

1 answer

1

Your problem is that you are getting the <option> 2 places:

                $sql_nome_for = "SELECT nome FROM cad_for WHERE id = '$cod_fornecedor'";
                $sql_nome_for = $pdo->query($sql_nome_for);
                $dado_for = $sql_nome_for->fetch();
                ?>

                <option><?php echo utf8_encode($dado_for['nome']); ?></option>

And then

                $sql_for = "SELECT id, nome FROM cad_for WHERE del <> '1' ORDER BY nome";

                $sql_for = $pdo->query($sql_for);

                If($sql_for->rowCount()>0){

                    foreach($sql_for as $fornecedor):
                        ?>
                    <option value="<?php echo $dado['id_cad_for']; ?>" ><?php echo utf8_encode($fornecedor['nome']); ?></option>

The most "straightforward" solution is probably:

  • remove option from first SELECT (check the data, but do not play in HTML);

    <option><?php echo utf8_encode($dado_for['nome']); ?></option>

  • enable selected attribute on second option :

    <option value="<?php
       echo $dado['id_cad_for'].($fornecedor['nome']==$dado_for['nome']?' selected':'');
    ?>"><?php echo utf8_encode($fornecedor['nome']); ?></option>
    
  • (I broke in lines just to make it easier to read)

    If for some reason the second select does not bring the desired name, or you prefer that the selected name is always the first one, you can do it differently:

  • maintains option of first SELECT ;

    <option><?php echo utf8_encode($dado_for['nome']); ?></option>

  • put a if in the second option to not show a repeated name:

    if ($fornecedor['nome']!=$dado_for['nome']) echo '<option value="'.
       $dado['id_cad_for'].'">'.utf8_encode($fornecedor['nome']).'</option>';
    
  • Taking advantage of this, it would be nice to fix either the DB or the page to eliminate utf8_encode , which is a function that only makes sense to match data coming from third parties.

        
    04.09.2018 / 05:11