Select in option

Asked

Viewed 95 times

-1

I have a screen of accounts to pay, and when I edit the data, in the option field is appearing the name of the provider (Liquigas), So far so good. But when I click on the list, the name of the supplier is appearing twice. What I might be doing 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>

inserir a descrição da imagem aqui

  • San, I put inside the loop, but now doubled the name.

  • Clone a condition in your query to not return the name of the first option that is before the loop, something like: $sql_for = "SELECT id, nome FROM cad_for WHERE del <> '1' and nome <> '".$dado_for['nome']."' ORDER BY nome";

  • But also put a value in that option out of the loop, otherwise when you save it will go empty. Something like: <option value="<?php echo $cod_fornecedor; ?>"><?php echo utf8_encode($dado_for['nome']); ?></option>

  • option worked, but value is not working, it is possible to source this screen for you San?

  • The value within the loop?

  • Yes, inside the loop

  • How do I leave the code of this screen here on the forum?

  • I put all the code.

  • Dude, inside the loop you put as value the $fornecedor['id']

  • You’re getting the name and id of the bank, right? The id is the value and the name is the text of the options. Nor should you edit the question.

  • The option inside the loop should look like this: <option value="<?php echo $fornecedor['id']; ?>" ><?php echo utf8_encode($fornecedor['nome']); ?></option>

  • It did not work San, sorry friend, your will is great to help me, but I will study the code better, maybe make a test with only this option, just to see where I’m going wrong, thank you very much for the help.

  • 1

    Quiet. See if the answer below helps, or comment there. Abs!

Show 8 more comments

1 answer

1


Your problem is you’re getting the <option> two-seat:

                $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 "direct" solution is probably:

  1. eliminate the option of the first SELECT (query the data, but not play in HTML);

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

  2. enable the attribute selected in the 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 happens select do not bring the desired name, or you prefer that the selected name is always the first, can do different:

  1. maintains the option of the first SELECT;

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

  2. puts a if in the second option not to show repeated name:

    if ($fornecedor['nome']!=$dado_for['nome']) echo '<option value="'.
       $dado['id_cad_for'].'">'.utf8_encode($fornecedor['nome']).'</option>';
    

Taking advantage, it would be good to fix either the DB or the page to delete the utf8_encode, which is a function that only makes sense to compatibilize data coming from third parties.

  • You have loaded the option, but you are not changing the vendor code.

Browser other questions tagged

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